FuelPHP - Modules


Module is a great way to write reusable web functionalities such as blog, album, chat, etc. Module does not disturb the other code in the web application. It lives in its own folder and silently provides its functionality. Modules are simply the same controller, models, and views except that they are grouped, configured, and placed in a special folder. Usually, a module typically resides within the application's sub-directory named modules located at fuel/app/modules.

Module Configuration

We can define the modules path in the main application configuration file, fuel/app/config/config.php as follows.

'module_paths' => array ( 
   path/to.’modules'.DS,              // path to application modules 
   path/to.’..’.DS.'globalmods'.DS    // path to our global modules 

Module Namespace

In FuelPHP, every module has its own PHP namespace. Setting separate namespace fixes the name clashes. For example, an employee module can be set under namespace, EmployeeModule as follows.

   namespace Employeemodule;  

   class Controller_Employee { 
      //code here 

Module must be named identical to the folder name of the module.

Module Structure

We can create a module by creating a folder name defined in the configuration. The folders name determines the name of the module, and the name of the namespace for the classes in the module.

The structure of the module is as follows −

  • classes
    • controller
    • model
    • view
  • config
  • lang
  • tasks
  • views

Module can have its own configuration files. It is very useful in the routing setup and it does not disturb the original configuration of the application. Another important concept is that, the module class can be reused by loading the module in the always_load configuration section as follows.

'always_load => array ( 
   'modules' => array('employeemodule'), 

Also, modules can be loaded and used instantly without configuring as follows.