Home Backend Development PHP Tutorial 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

Jul 13, 2016 am 10:24 AM

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:

1

2

'APP_GROUP_LIST'=>'Home,Admin',

'DEFAULT_GROUP'=>'Home',

Copy after login

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):

1

2

3

4

5

6

7

8

9

10

─Home Home分组目录

 ├─Common 分组函数目录

 ├─Conf 分组配置目录

 ├─Lang 分组语言包目录

 ├─Action 分组Action控制器目录

 ├─Model 分组Model模型目录

 ├─Widget 分组Widget目录

 ├─ORG 分组扩展类库目录

 ├─... 其他分层目录

 └─Tpl 分组模板目录

Copy after login

(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

1

'APP_GROUP_MODE'=>1

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

├─index.php   项目入口文件

 ├─Common 项目公共文件目录

 ├─Conf 项目配置目录

 ├─Lang 项目语言目录

 ├─Modules 独立分组目录

 │ ├─Home Home分组目录(独立分组目录结构参考前面)

 │ ├─Admin Admin分组目录

 │ └─... 其他分组目录

 ├─Lib 分组公共类库目录

 │ ├─Action 公共Action类库目录

 │ ├─Behavior 公共行为类库目录

 │ ├─Model 公共模型类库目录

 │ └─... 其他公共类库目录

 ├─Runtime 项目运行时目录

 │ ├─Cache 模板缓存目录

 │ ├─Data 数据缓存目录

 │ ├─Logs 日志文件目录

 │ └─Temp 临时缓存目录

Copy after login


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:

1

Tpl/Home/Index/index.html

Copy after login

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

1

Tpl/Index/index.html

Copy after login

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:

1

2

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

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

Copy after login

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:

1

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

Copy after login

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles