A slightly larger project, if developed according to the webapp generated by yii. All controllers are placed under the controllers folder, and all models are placed under the models folder. If you have n multiple controllers and n multiple models, maintaining the code in this case will be a very painful process. To avoid this situation, Yii provides a directory structure of Modules.
Modules (module) is an independent unit, including views, controllers and other components. The difference between it and an application is that it cannot be deployed separately. Modules are stored in the module directory of the application.
Your project can be divided into n multiple Modules, and each Module has its own controllers and models. Such an organizational structure makes development and management much more convenient and concise.
Modules in YII are very flexible, and a module can contain submodules. In theory, modules can be infinitely nested.
Directory structure of the module (The directory structure generated below is used as an explanation)
modules module storage directory
└── admin is a module. The name of the module corresponds to the name of the directory, which is unique. It is also the moduleid
in routing
├── components components used in the module
├── controllers includes controllers
│ └── DefaultController.php Default Controller
├── messages internationalization
├── models model class files
├── AdminModule.php module class file
└── views view file
├── default default view
│ ├── index.php view file
└── layouts contains layout files
The basic directory structure is as above, of course you can add some customized things yourself.
How to create a module (Here we create the module through the gii generator that comes with yii)
Create the basic structure through the gii generator that comes with yii. The way to enable gii is to modify the following content in your application config/main.php file:
<?php ...... 'modules'=>array( 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'123456',//你的密码访问时需要输入 'ipFilters'=>array('127.0.0.1','::1'), ), ), ......
Then visit the url your application/index.php?r=gii. Visit gii, open it and select the Module Generator option on the left menu. You will see the following screen
Enter the name of the module in Module ID, I entered admin here, and then click the Preview button. As shown below, it shows you all the files that will be generated, allowing you to preview them before creating them:
Then click the Generate button to generate all files. Because the web server process requires write access, make sure your /protected folder is writable by the application.
Configure to use this module
We configure the main configuration file protected/config/main.php. The following code needs to be modified and 'admin' is added:
...... 'modules'=>array( 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'你的密码', ), 'admin', ), ......
After saving the above modifications, our new admin module is ready for use. We can access the module we created at:
yourapp/index.php?r=admin/default/index
Use layout
in the moduleWhen we visit index.php?r=admin/default/index, we will find that the module uses the /protected/views /layouts/main.php file under your application, and we may want to use /protected/ modules/admin/views/layouts /main.php file, allowing the admin module to have an independent layout view. We can find you at:
protectedmodulesadmincontrollersDefaultController.php Add the following code.
public $layout='application.modules.admin.views.layouts.main';
We copy from /protected/views/layouts/main.php to /protected/modules/admin/views/layouts/ and make slight modifications so that the module has an independent layout view.
Using Assets in modules
添加新的模块时,一般会包含图像文件,CSS文件,JavaScript文件等。
模块可以直接从网站主目录中引用。但是如果想要创建一个模块能够在任何地方引用,并且能够避免命名冲突,就要用到assets了。
过程是(这里模块名是admin):
1、把需要用到的资源放在modules/admin/assets下。
2、然后通过 CAssetManager,Yii::app()->assetManager 能够自动的将私有资源 publish 到公共目录下网站目录 /assets
3、Yii 会自动在网站目录的 /assets 下创建一个随机不冲突的文件夹,如 2b31b42b,并把你的modules/admin/assets目录下的文件拷贝过去。
例如我的模块是Admin,文件路径通过如下代码获得,修改protected\modules\admin\AdminModule.php文件。
class AdminModule extends CWebModule{ private$_assetsUrl; public function getAssetsUrl(){ if($this->_assetsUrl===null) $this->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.modules.admin.assets')); return$this->_assetsUrl; } public function setAssetsUrl($value){ $this->_assetsUrl=$value; } }
然后,在 /protected/modules/admin/views/layouts/main.php 中使用 $this->module->assetsUrl 就可以调用你的css等文件了。模板文件的代码如下:
<link rel="stylesheet" type="text/css" href="<?php echo $this->module->assetsUrl; ?>/css/screen.css" />
4,通过如上操作,该模块只要把admin目录拷贝,就可以多次复用了。
模块的配置,使用方法
在配置文件 /config/main.php 中:
配置文件中也可以及添加对模块中属性初始化的参数例如:
...... 'modules'=>array('admin'=>array('web_url'=>'www.phpernote.com'), ......
对应在 Controller 中的访问方式是:
Yii::app()->controller->module->web_url;