Working with Yii framework modules
Intro
In this post I will write about Yii modules. You should have some basic understanding in the following sections
- You know what is Yii
- You know what is Model, View and Controller
- You know what is Gii
What is module
As per official Yii’s documentation, a module is:
a self-contained software unit that consists of models, views, controllers and other supporting components. In many aspects, a module resembles to an application. The main difference is that a module cannot be deployed alone and it must reside inside of an application. Users can access the controllers in a module like they do with normal application controllers
Most websites today consist of several parts, at least front-end and back-end part. One possible approach to implement this website is to create two different Yii framework modules. Let’s name the first module “front-end”, and the second one “admin”.
If you decide to develop this kind of website using Yii modules, then here is the list of benefits:
- You can configure every module separately
- Every module can define different set of components
- You can define different login url for every module
- Define different route rules for every model
- Every module has its own Model, Controller and View files
- Every module can have different layout
- Use one module inside other module
Create a new module
To create a new module, open Gii and chose option to create a new Module. Gii is really powerful tool which enables you to create not only modules, but Models, CRUD operations and Controllers. Some other PHP frameworks don’t have this ability.
When you create a new module in application, you created an application in application (Yii module is just a new application). This means, that you can configure module, like you configure Yii application (you can override default configuration).
For example, you would like to create different login for front-end users and back-end users, or to change layout for front-end users, and backe-end users.
Yii module registration
When you create a new module, you have to register it in the main application configuration file (main.php):
1 2 3 4 5 6 7 8 9 10 11 12 13 |
'modules'=>array( // uncomment the following to enable the Gii tool 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'12345', // If removed, Gii defaults to localhost only. Edit carefully to taste. 'ipFilters'=>array('127.0.0.1','::1'), ), 'admin'=>array( 'class'=>"app.modules.admin" ) ), |
As you can see, I registered module called “Admin” in the main Yii application configuration file. Please note that Gii, is also implemented as Yii module.
Module configuration
When you create a new module using Gii too, inside the Yii’s application folder, Gii will create a new folder called modules
which will contain module files. In my example, folder modules
contains folder called admin
which contains all files (view, controller, model, layout files) for admin
module.
When you open admin
folder, there is module class file in my case called AdminModule.php. By opening this file, you can configure and override default configuration for the admin
module.
Let’s see how to override default configuration for the admin
module:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function init() { parent::init(); Yii::app()->setComponents(array( 'administrator'=>array( 'class' => 'CWebUser', // enable cookie-based authentication 'allowAutoLogin'=>true, 'baseUrl'=>Yii::app()->createUrl("admin/user/login"), 'stateKeyPrefix' => '_admin', ), ), false); } |
In the code above,we defined a new administrator
component with class CWebUser
and allowAutoLogin
attribute set to true, andbaseUrl
set to Yii::app()->createUrl("admin/user/login")
. This way, when administrator logs out from the admin panel module, he/she will be redirected to the URL defined in this configuration, not in the configuration in the main.php.
Please notice that the second parameter to the setComponent
of the CModule
class is set to false
. Here is meaning of the second parameter:
Whether to merge the new component configuration with the existing one. Defaults to true, meaning the previously registered component configuration of the same ID will be merged with the new configuration. If false, the existing configuration will be replaced completely.
Now, anywhere in the modul’s controller we can call administrator
component by typing:
1 2 3 |
Yii::app()->getModule("admin")->administrator->login($this->_identity,$duration); Yii::app()->getModule("admin")->administrator->logout(); |
or
1 2 3 |
Yii::app()->administrator->login($this->_identity,$duration); Yii::app()->administrator->logout(); |
How to create links in Yii modules
Yii is powerful and smart framework, which enables you to create a link inside modules just like you do it in the normal application.
For example, if you execute the following command in the controller of the admin
module:
1 |
$this->createUrl("user/showProfile", array("id"=>23)); |
than the following link will be created: http://mywebsite.com/admin/user/showProfile/id/23
Don’t write something like:
1 |
$this->createUrl("admin/user/showProfile", array("id")=>23)); |
if you are creating your link in controller which belongs to the admin
module, Yii will automatically conclude that you want to create link belonging to the admin
module, and it will automatically add necessary moduleID value in the URL. This is really helpful if you in future want to change the module name.
If you are in some other module, and want to create link to the admin
module, then use this code to create a link:
1 |
$this->createUrl("admin/user/showProfile", array("id")=>23)); |