The YII framework is a high-performance PHP framework for developing large Web applications based on components. Yii provides almost everything you need for today's Web 2.0 application development. Yii is one of the most efficient PHP frameworks. Yii was the founder of Manggold J's painstaking efforts, 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, all the model is placed under the Models folder, if you have more than n controller and N-more model, in this case maintaining the code will be a very painful process. To avoid this situation, YII provides a directory structure of Modules (modules).
Modules (module) is a standalone unit that contains views, controllers, and other components, and it differs from an application in that it cannot be deployed separately and that the modules are stored in the application's Moudules directory.
Your project can be divided into N multiple module, then each module has its own controllers and models. Such organizational structure, whether it is development, management should be convenient and concise many.
The modules in Yii are very flexible, and a module can contain sub-modules. Theoretically, the module can be infinitely nested.
The directory structure of the module (described here as the directory structure generated below)
Storage directory for Modules modules
└──admin a module, the name of the module corresponds to the name of the directory, which is unique. is also a moduleid in routing
Components used in the ├──components module
├──controllers contains controller
│└──defaultcontroller.php Default Controller
├──messages Internationalization
├──models Model class file
class files for ├──adminmodule.php modules
└──views trying to 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 custom things on your own.
How to create a module (here we create the module through Yii's own GII generator)
To create a basic structure through YII's own GII generator, the way to open the GII is to modify the following in your app config/main.php file:
<?php ... ' Modules ' =>array ( ' gii ' =>array (' class ' = ' System.gii.GiiModule ', ' password ' = ' 123456 ',/ /Your password must be accessed by entering ' ipfilters ' =>array (' 127.0.0.1 ', ':: 1 '),),
Then visit the URL of your app/index.php?r=gii access GII, open a later selection, the Module Generator option on the left menu. You'll see the picture below
In module ID, enter the name of the modules, I enter admin here, and then click the Preview button. As shown, it shows you all the files that will be generated, allowing you to preview them before you create them:
Then click the Generate button to generate all the files. Because the WEB server process requires Write permissions, make sure that your/protected folder is writable for the application.
Configure the use of this module
We configure the main configuration file protected/config/main.php, as the following code needs to be modified to add ' admin ':
' Modules ' =>array ( ' gii ' =>array (' class ' = ' System.gii.GiiModule ', ' password ' = ' Your password ', ), ' admin ',
After saving the above changes, our new admin module is ready to use. We can access the modules we created using the following address:
Your application/index.php?r=admin/default/index
Using layout in a module
Our visit to Index.php?r=admin/default/index will find that the module uses the/protected/views/layouts/main.php file under your app, and we may want to use/protected/ modules/admin/views/layouts/main.php file, allowing the admin module to have a separate layout view. We can do this in:
protected\modules\admin\controllers\defaultcontroller.php Add the following code.
Public $layout = ' Application.modules.admin.views.layouts.main ';
We copied the copy from/protected/views/layouts/main.php to/protected/modules/admin/views/layouts/and changed it slightly so that the module had a separate layout view.
Using assets in a module
When adding a new module, it will typically include image files, CSS files, JavaScript files, and so on.
Modules can be referenced directly from the Web site home directory. But if you want to create a module that can be referenced anywhere, and you can avoid naming conflicts, you need to use assets.
The process is (here the module name is Admin):
1, the need to use the resources under the modules/admin/assets.
2, then through the Cassetmanager,yii::app ()->assetmanager can automatically publish the private resources to the public directory under the site Directory/assets
3. Yii will automatically create a random and non-conflicting folder under the/assets of the site directory, such as 2b31b42b, and copy the files from your Modules/admin/assets directory in the past.
For example, my module is admin, the file path is obtained by 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; } }
Then, you can call your CSS and other files using the $this->module->assetsurl in/protected/modules/admin/views/layouts/main.php. The code for the template file is as follows:
<link rel= "stylesheet" type= "Text/css" href= "<?php echo $this->module->assetsurl;? >/css/screen.css"/ >
4, through the above operation, the module as long as the admin directory copy, it can be reused multiple times.
Configuration of modules, how to use them
In the configuration file/config/main.php:
In the configuration file, you can also add parameters to initialize properties in the module, for example:
' Modules ' =>array (' admin ' =>array (' web_url ' = ' www.phpernote.com '),
The corresponding access method in the Controller is:
Yii::app ()->controller->module->web_url;
As programmers, we need to know that Yii is a high-performance, component-based PHP framework for developing large Web applications. Yii is written in strict OOP, with well-established library references and comprehensive tutorials. From mvc,dao/activerecord,widgets,caching, hierarchical Rbac,web services, to theming, i18n and l10n,yii provide almost everything you need for today's Web 2.0 application development. In fact, Yii is one of the most efficient PHP frameworks.
Hopefully this section will allow you to gain more from the YII framework.