Yii framework module development analysis_PHP tutorial

WBOY
Release: 2016-07-13 09:54:23
Original
825 people have browsed it

Yii framework module development analysis

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'),
	),
),
......
Copy after login

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

Yii framework框架之模块开发分析-生成目录

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:

Yii framework框架之模块开发分析-预览生成目录

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',
),
......
Copy after login

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 module

When 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;
	}
}
Copy after login

然后,在 /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" />
Copy after login

4,通过如上操作,该模块只要把admin目录拷贝,就可以多次复用了。

模块的配置,使用方法

在配置文件 /config/main.php 中:

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

......
'modules'=>array('admin'=>array('web_url'=>'www.phpernote.com'), 
......
Copy after login

对应在 Controller 中的访问方式是:

Yii::app()->controller->module->web_url;

您可能感兴趣的文章

  • php用header()实现文件下载,下载的文件提示被破坏不能打开的解决办法
  • CuteFTP连接ftp服务器时弹出“遇到无效的参数”错误的解决办法
  • php smarty中文截取插件开发示例
  • 关于使用in_array() foreach array_search() 查找数组是否包含时的性能对比
  • linux chmod(文件或文件夹权限设定)命令参数及用法详解
  • iframe 子页面如何操作父页面元素
  • javascript实现刷新iframe的方法的总结
  • array_walk 和 foreach, for 的效率的比较,php性能优化

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/997441.htmlTechArticleYii framework框架之模块开发分析 一个稍微大点的项目,如果按照yii生成的webapp进行开发。所有的controller放到controllers文件夹下,所有的mode...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template