Monday, April 9, 2012

Pyrocms - Creating hello world module in pyrocms

Custom module development :-

Hi all, today i am gonna show you how you can create a simple "hello world" module in pyrocms. You can do so by following some simple steps mentioned in this article.


Here are these steps,

1. download pyrocms package from pyrocms website and install it in your wamp\www directory.

2. Next create a folder "helloworld" in \www\pyrocms\addons\shared_addons\modules\

3. As pyrocms is based on codeignitor which is php framework and using MVC ( model, view, controller ) architecture, so each pyrocms module will have these folders in it. So create "controllers", "models" and "views" folders in "helloworld" folder.

After that our module directory will look something like this,


4. Next create details.php in your module's same directory. details.php file contains all the information about module like its version, title, description etc. This file also contains functions to install, uninstall and upgrade the module. In our case details.php file will look like this,


 <?php  
 class Module_HelloWorld extends Module {  
     public $version = '1.0';  
     public function info(){  
         return array(  
             'name' => array(  
                 'en' => 'Hello World',  
             ),  
             'description' => array(  
                 'en' => 'This is a Hello World Module.',  
             ),  
             'frontend' => TRUE,  
             'backend' => FALSE,  
             'menu' => 'content'  
         );  
     }  
     public function install()    {  
         return true;  
     }  
     public function uninstall(){  
         return true;  
     }  
     public function upgrade($old_version){  
         // Your Upgrade Logic  
         return TRUE;  
     }  
     public function help(){  
         // Return a string containing help info  
         // You could include a file and return it here.  
         return "No Help";  
     }  
 }  
 ?>  

Simply copy/paste and save this code in your details.php file.

5. Next create a file called "helloworld.php" in controllers folder and put following code in it,


 <?php  
 class HelloWorld extends Public_Controller {  
     public function __construct(){  
         parent::__construct();  
     }  
     function index() {  
         $this->data->displayMessage = "Hello World";  
         $this->template->build('display',$this->data);  
     }  
 }  
 ?>  


 This file is the controller file of your module and will have the same name as your module name. You can notice that this class is extending "Public_Controller" class because this module is for front-end. If you want to put some logic for admin then you have to extend "Admin_Controller" class. In our case no need to do so.

6. Next create display.php file in views folder and put following code in it,


 <?php  
 echo $displayMessage;  
 ?>  


That's it. Now you can test your module by running the url like this (this url is in my case),

http://localhost/pyrocms/index.php/helloworld

7 comments:

  1. This is the simplest explanation of how to create a new module in PyroCMS, and it gets to the point.

    Thanks and keep it up

    ReplyDelete
  2. thanks Asad, very nice tutorial. look forward to more of these.

    ReplyDelete
  3. i have got some error during the module install or running the module in the browser.

    A PHP Error was encountered
    Severity: Warning
    Message: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\pyrocms\addons\shared_addons\modules\helloworld\details.php:33)

    Filename: drivers/Session_cookie.php

    ReplyDelete
    Replies
    1. This may be because of some error before redirecting or may be you have white space in start of file before <?php tag. So check you files for that most probably in all files of this module or index files of your project

      Delete
  4. Hi Asad, Can you help me out how to integrate my own module into pyrocms running project, and i want to run whole pyrocms running project on my localhost

    ReplyDelete
  5. i have a problem it says :page missing

    ReplyDelete

Please feel free to post your comments. If anyone has a good article or good thing to share, just send me that with your name to asadmehmoodstar@gmail.com. and if anyone want so receive updates regarding my blog, he can subscribe to my weekly newsletter on "Subscribe to our mailing list" section.

Thanks