Fork me on GitHub
 
 

Modularizing Fuel PHP Framework

Written by Aravind H.U on Jul 12, 2014

When ever we design a system we prefer making the code as much scalable as possible, and as much re usable as possible, and as much readable as possible so that any collaboration can run with out any hurdles. To achieve that in any application, we can have modules.

A module is all related code and assets, that solve a specific problem. Modules should be designed in such a way so that it could be used as plug and play.

In FuelPHP every module lives in its own PHP namespace.

A module can also be a full fledged MVC application, but not always.

Fuel PHP didn't had documentation initially on configuring modules, I found it very difficult to achieve this configuration, I think community has updated the documentation now and also there is an Oil generator which could be used to generate a module.

I decided to create a FuelPHP skeleton application by configuring modules. You can fork/download my github repository from the below URL

modular-fuelphp

Some Implementation guidelines

To create new routes to module controller action we need to follow the below convention in app/config/routes.php file.

    
        'route-name' => 'module-name/controller-name/action-name',
    

Also when ever you are using extending controller class or controllerTemplate class you need to refer to root namespace . I meant you need to add "\"

For example



            class Controller_Welcome extends \Controller
            {

            }

            // OR if you are using controller template

            class Controller_welcome extends \Controller_Template
            {

            }

        

Also when forging views and presentations you need to add a back slash to forge .

For example

    


        class Controller_Welcome extends \Controller
        {

        /**
        * The basic welcome message
        *
        * @access public
        * @return Response
        */
        public function action_index()
        {

        return \Response::forge(\View::forge('welcome/index'));
        }
        }

    
        

Once module is created you can reuse them in another module by loading them at the module bootstrap.

In FuelPHP module bootstrap is nothing but the before() function which gets called before calling anything in the controller.



            /**
            * This is before controller level bootstrap
            */
            public function before() {
            \Module::load('student');
            \Module::load('dashboard');

            }

        

Once used you can just refer to the module name space followed by class name to instantiate and call methods in model classes.

Share this:

Disclaimer

I hope that you won’t object if I occasionally use contractions and the less formal “you” instead of “one” in my posts presented here. That makes writing it easier for me and ensures a less formal atmosphere.

I attempt to post articles here as carefully as possible, but I cannot guarantee absolute freedom from errors in general at this time. Please bear with me. If you would like to help me please contact me

The information contained in this website is for general information purposes only. I try to keep the information up to date and correct, I make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the website for any purpose.

Any reliance you place on such information is therefore strictly at your own risk.

×