Home  >  Article  >  Backend Development  >  Yii framework framework module development

Yii framework framework module development

小云云
小云云Original
2017-11-16 10:56:421533browse

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    
   ......    
  &#39;modules&#39;=>array(    
  &#39;gii&#39;=>array(    
 &#39;class&#39;=>&#39;system.gii.GiiModule&#39;,    
  &#39;password&#39;=>&#39;123456&#39;,//你的密码访问时需要输入    
 &#39;ipFilters&#39;=>array(&#39;127.0.0.1&#39;,&#39;::1&#39;),    
  ),    
  ),

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:

 &#39;modules&#39;=>array(    
 &#39;gii&#39;=>array(    
  &#39;class&#39;=>&#39;system.gii.GiiModule&#39;,    
&#39;password&#39;=>&#39;你的密码&#39;,    
 ),    
 &#39;admin&#39;,    
),

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(&#39;application.modules.admin.assets&#39;));    
 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 中:

配置文件中也可以及添加对模块中属性初始化的参数例如:

 &#39;modules&#39;=>array(&#39;admin&#39;=>array(&#39;web_url&#39;=>&#39;www.phpernote.com&#39;),

对应在 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框架上有更多的收获。

相关推荐:

简单介绍Yii2的使用场景

推荐10款安装Yii源码(收藏)

PHP的Yii框架中的属性Property

yii2框架的下载安装图文教程

Yii2利用表单进行文件上传的实例讲解

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn