Yii Framework is a component-based, high-performance PHP framework for developing large-scale web applications. Yii provides almost everything needed for today's Web 2.0 application development. Yii is one of the most efficient PHP frameworks. Yii is the brainchild of founder Xue Qiang and began development on January 1, 2008.
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.
The directory structure of the module (the directory structure generated below is used as an explanation)
modules The storage directory of the module
└── admin A module, the name of the module corresponds to the name of the directory ,only. It is also the moduleid
in routing ├── components components used in the module
├── controllers include controllers
│ └── DefaultController.php default controller
├── messages internationalization
├── models model class file
├── 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 use the gii generator that comes with yii to create the module)
Create the basic structure through the gii generator that comes with yii. The method to turn on gii is 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 enter 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 a new one:
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.
Configuration using 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 through the following address:
yourapp/index.php?r=admin/default/index
Use layout in the module
us Visit index.php?r=admin/default/index and you 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 allows the admin module to have an independent layout view. We can add the following code in:
protected\modules\admin\controllers\DefaultController.php.
public $layout='application.modules.admin.views.layouts.main';
We copy from /protected/views/layouts/main.php to /protected/modules/ admin/views/layouts/ , slightly modified so that the module has an independent layout view.
Using Assets in a module
When adding a new module, it will generally include image files, CSS files, JavaScript files, etc.
Modules can be referenced directly from the website's main directory. But if you want to create a module that can be referenced anywhere and avoid naming conflicts, you need to use assets.
The process is (the module name here is admin):
1. Put the resources you need to use under modules/admin/assets.
2. Then through CAssetManager, Yii::app()->assetManager can automatically publish the private resources to the public directory website directory/assets
3. Yii will automatically Create a random non-conflicting folder under the /assets directory, such as 2b31b42b, and copy the files in your modules/admin/assets directory.
For example, my module is Admin. The file path is obtained through the following code. Modify the protected\modules\admin\AdminModule.php file.
class AdminModule extends CWebModule{ private$_assetsUrl; $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;
作为程序员,我们要知道,Yii是一个基于组件的高性能PHP框架,用于开发大型Web应用。Yii采用严格的OOP编写,并有着完善的库引用以及全面的教程。从 MVC,DAO/ActiveRecord,widgets,caching,等级式RBAC,Web服务,到主题化,I18N和L10N,Yii提供了今日Web 2.0应用开发所需要的几乎一切功能。事实上,Yii是最有效率的PHP框架之一。
希望本节内容能让大家在Yii框架上有更多的收获。
相关推荐:
The above is the detailed content of Yii framework framework module development. For more information, please follow other related articles on the PHP Chinese website!