Home  >  Article  >  Backend Development  >  Decrypt the independent grouping function application of ThinkPHP version 3.1.2_PHP tutorial

Decrypt the independent grouping function application of ThinkPHP version 3.1.2_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:24:25794browse

ThinkPHP’s grouping function is a function of great practical value that is widely used by developers. This function can solve the problem of too many MVC layered files that are difficult to manage in medium and large projects.

The independent grouping function added in ThinkPHP 3.1.2 version proposes new solutions to such problems, and is more suitable for the component-based development model. Let’s take a look at this feature.

1. Overview

The independent grouping function does not affect the operation of the original grouping mode, and the original grouping mode only needs to move the directory structure to complete the upgrade of the independent grouping mode without any changes to the application code.

And the new independent groups can be easily loaded, unloaded and moved independently, which can get rid of the original problem of ordinary group files being scattered in different directories.

The URL access of independent groups is the same as that of the original ordinary groups. There is no difference. The configuration group list is still configured using the APP_GROUP_LIST parameter. To set the default group, use the DEFAULT_GROUP parameter. For example:

'APP_GROUP_LIST'=>'Home,Admin',
'DEFAULT_GROUP'=>'Home',

Although the new independent grouping can completely replace the original ordinary grouping mode, in order to consider the smooth upgrade of the original grouping project, this new version adds a configuration parameter:

APP_GROUP_MODE is used to configure the grouping mode. The default is 0, which is the original normal grouping mode. If set to 1, it means enabling the independent grouping mode.
Whether you need to upgrade to independent group mode is entirely up to you. I believe you will make a wise choice after reading the following content.

2. Directory structure

After enabling independent grouping mode, you need to create an independent grouping directory under the project directory. This directory can be configured by the project configuration file through the APP_GROUP_PATH parameter. The default value is Modules. Assuming we have not made any changes, under the Modules directory are the subdirectories of each group. Each group is completely independent, including models, controllers, views, configuration and function files, etc. You can easily implement grouping moving and uninstalling.
The standard independent group directory structure is (taking a Home group as an example):

─Home Home分组目录
 ├─Common 分组函数目录
 ├─Conf 分组配置目录
 ├─Lang 分组语言包目录
 ├─Action 分组Action控制器目录
 ├─Model 分组Model模型目录
 ├─Widget 分组Widget目录
 ├─ORG 分组扩展类库目录
 ├─... 其他分层目录
 └─Tpl 分组模板目录

(Note: Independently grouped directory structures currently need to be created manually )
Basically, you can see that apart from the independent group having no entry file, the structures of other independent projects are basically in place.
To upgrade from the original ordinary group to an independent group, you only need to add:

in the project configuration file
'APP_GROUP_MODE'=>1

Then put the MVC files belonging to the corresponding group under the original project Lib directory, as well as the group's function, configuration and language (if any) files in sequence and put them into the corresponding directory according to the directory structure of the independent group above.

3. Public files

After using independent grouping, the original project Lib directory is designed as a grouped public class library file. If your multiple independent groups need to call a common Action or Model class (actually it also includes other hierarchical controllers and models class), you can put these public classes into the corresponding directory under the Lib directory of the project (during the actual upgrade process, these public class library files basically keep the directory structure unchanged, so there is no need to move).
Grouped public class library files do not need to be loaded manually, and all use an automatic loading mechanism.
Therefore, the final actual project directory structure using independent grouping mode is as follows:

├─index.php   项目入口文件
 ├─Common 项目公共文件目录
 ├─Conf 项目配置目录
 ├─Lang 项目语言目录
 ├─Modules 独立分组目录
 │ ├─Home Home分组目录(独立分组目录结构参考前面)
 │ ├─Admin Admin分组目录
 │ └─... 其他分组目录
 ├─Lib 分组公共类库目录
 │ ├─Action 公共Action类库目录
 │ ├─Behavior 公共行为类库目录
 │ ├─Model 公共模型类库目录
 │ └─... 其他公共类库目录
 ├─Runtime 项目运行时目录
 │ ├─Cache 模板缓存目录
 │ ├─Data 数据缓存目录
 │ ├─Logs 日志文件目录
 │ └─Temp 临时缓存目录


4. Template file

The template files of independent groups are moved from the Tpl directory of the project to the Tpl directory of the independent group directory. The original template group subdirectory is no longer needed, for example:

Tpl/Home/Index/index.html 

After moving to the Tpl directory under the independent group, it should be:

Tpl/Index/index.html

Template theme function is still supported.

5. Call the class library

When importing class libraries for independent groups, the usage method is basically the same as importing project class libraries, for example:

import('@.Action.TestAction'); // 导入当前分组下的Action/TestAction.class.php
 import('@.ORG.Util.Image'); // 导入当前分组下的ORG/Util/Image.class.php

Independent groups do not consider interactions and calls between multiple groups, and can only call public class libraries.
If you must call other grouped class libraries without using a common class library design, you can use:

import('ORG.Util.Image',APP_PATH.'Modules/Admin'); 

However, after adopting independent grouping, method A, method R, and method D do not support cross-group calls.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825443.htmlTechArticleThinkPHP’s grouping function is a function of great practical value that is widely used by developers. This function can solve In medium and large projects, there are too many MVC layered files that are difficult to manage...
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