Thursday, July 5, 2012

Using modules in Yii

What is modules?
Module is separate piece of application that can have Models, Controller,Views, modules are placed in protected/modules directory. We can define login system for modules.Module cannot be deployed alone it needs application to run.
  • Modules can be nested in unlimited levels
  • Modules may have separate login system

Application directory structure

 protected
   /commands
   /components
   /config
   /controllers
   /models
   /modules
       /admin       
       /user
       /agent

We have three modules placed under modules directory namely admin,user,agent. if we look in to the module directory structure we can find many files and folders similar to application.

   /modules
       /admin    
          AdminModule.php
          components/                
          controllers/                       
               DefaultController.php   
          extensions/                      
          models/                    
         views/                     
         layouts/               
         default/              
             index.php            


Why we have to use modules?
In large scale application we may divide our application in to many modules so that they can be resused later and it makes application maintenance easier.

Creating a Modules
Enable Gii in application config, open Gii generator using yourapplication/?r=gii/default/login in browser addresbar, then type your password,
'modules' => array(
        // uncomment the following to enable the Gii tool
        'gii' => array(
            'class' => 'system.gii.GiiModule',
            'password' => 'giitest',
        ),
         .......
click module generator enter your module name that's all yii will do the rest.
Gii module generator


Using Module
To use modules in our application, we have to configure it in application config file
'modules' => array(
        // uncomment the following to enable the Gii tool
        'gii' => array(
            'class' => 'system.gii.GiiModule',
            'password' => 'giitest',
        ),
        'user', //user module
        'admin', //admin module
        'agent', //agent module
    ),

Nested Module
Module can have any number of nested module.one module can contain another module previous module called parent module later module called child module so one can access child module action like. parentModuleID/childModuleID/controllerID/actionID
'modules'=>array(
    'module1'=>array(
        'modules'=>array(
            'module2',
            'module3'
        )
    )
)

 Sharing Models/Components/extensions to Modules

To share application's models/components/extensions to module, just import them using setImport function.

Here we are sharing models from user module, so admin and user model will use same model across application

class AdminModule extends CWebModule
{
 public function init()
 {
  // this method is called when the module is being created
  // you may place code here to customize the module or the application

  // import the module-level models and components
  $this->setImport(array(
   'admin.models.*',
   'admin.components.*',
   'user.models.Profile',      //sharing models from user module
   'user.models.TransactionHistory',
                        'agent.models.*',
  ));
               .....


Sharing Layouts to Modules

To share a main layout to module controller

class AdminController extends Controller
{ 
 public $layout='//layouts/admin';
...

the above statement uses admin template from protected/views/layouts/admin

// uses application views folder, refers to "protected/views/layouts"
/ uses module views folder refers to "protected/modules/module/views/layouts"

Creating Login system for Modules
Refer my previous article Module based login 


Pointing Subdomains to modules

Point sub-domain to module

This tips from the forum URL Subdomain to module

There is a feature called "Parameterizing Hostnames" (described on this pages). add the following lines to main.php

'components' => array(
   ...
   'urlManager' => array(
      'rules' => array(
         'http://customer.example.com' => 'customer/default/index',
         'http://customer.example.com/login' => 'customer/default/login',
      ),
   ),
   ...
),

So "customer/default/index" refers to the module "customer" with controller "default" and action "index".>

4 comments:

Lokashree sam said...

It was a great information and Its really worth reading it. The author did an mind blowing work by describing each and every concept in detail. Thanks for such an informative post. Please keep up your good work.
Engineering Colleges In Chennai, Nautical Science Colleges In Chennai

Lokashree sam said...

It was a great information and Its really worth reading it. The author did an mind blowing work by describing each and every concept in detail. Thanks for such an informative post. Please keep up your good work.
Engineering Colleges In Chennai, Nautical Science Colleges In Chennai

Prasanth said...

A very well-written post. I read and liked the post and have also bookmarked you. All the best for future endeavours.Getting some solution regarding.
UI Design Company Bangalore, Web Application Development Services in Bangalore

Radley Co Tad said...


I have read your blog its very attractive and impressive. I like it your blog.

PHP Training in chennai | PHP Training Course

PHP Training in chennai | Online PHP Course