


Decrypt the independent grouping function application of ThinkPHP version 3.1.2_PHP tutorial
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 |
|
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 |
|
(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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
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 |
|
After moving to the Tpl directory under the independent group, it should be:
1 |
|
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 |
|
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 |
|
However, after adopting independent grouping, method A, method R, and method D do not support cross-group calls.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

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.

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,

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 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? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

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.

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