Decrypt the application of independent grouping function in ThinkPHP version 3.1. 2

  • 2021-07-02 23:38:46
  • OfStack

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

However, the independent grouping function added in ThinkPHP version 3.1. 2 puts forward a new solution to this kind of problem, which is more suitable for the component-based development mode. Let's learn about this 1 function.

1. Overview

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 independent grouping mode without any change of application code.

Moreover, the new independent grouping can be conveniently loaded, unloaded and moved independently, and can get rid of the trouble that the original ordinary grouping files are scattered under different directories.

There is no difference between the URL access of independent packets and the original ordinary packet 1, and the configuration packet list is still configured with APP_GROUP_LIST parameters. Setting the default grouping takes 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 items, 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 it is set to 1, it means that the independent grouping mode is enabled.
Whether you need to upgrade to independent grouping mode is entirely up to you. I believe you will have a wise choice after reading the following content.

2. Directory structure

When the standalone grouping mode is enabled, you need to create a standalone grouping directory under the project directory, which can be configured by the project configuration file through the APP_GROUP_PATH parameter, and the default value is Modules. Assuming we haven't made any changes, under the Modules directory is a subdirectory of each group. Each group is completely independent, including model, controller, view, configuration and function files, etc. You can easily move and uninstall groups.
The standard independent packet directory structure is (take an Home packet as an example):


 - Home Home Group directory 
  - Common  Group function directory 
  - Conf  Grouping configuration directory 
  - Lang  Group Language Pack Directory 
  - Action  Grouping Action Controller directory 
  - Model  Grouping Model Model catalog 
  - Widget  Grouping Widget Directory 
  - ORG  Grouping Extended Class Library Directory 
  - ...  Other hierarchical directories 
  Off- Tpl  Grouping template directory 

(Note: The independently grouped directory structure currently needs to be created manually.)
Basically, we can see that the independent grouping has basically the structure of other independent projects except that there is no entry file.
To upgrade from normal grouping to independent grouping, you only need to add:


'APP_GROUP_MODE'=>1

Then the original project under the Lib directory belongs to the corresponding grouping of MVC files, as well as grouping functions, configuration and language (if any) files in turn against the above independent grouping directory structure can be put into the corresponding directory.

3. Public documents

After adopting independent grouping, The original project Lib directory was designed as a grouped public class library file, If your multiple independent groups need to call common Action or Model classes (including other hierarchical controllers and model classes), you can put these common classes in the corresponding directory under the Lib directory of the project (in the actual upgrade process, these common class library files basically keep the directory structure unchanged, so there is no need to move them).
Grouped public class library files do not need to be loaded manually, but all adopt automatic loading mechanism.
Therefore, the actual project directory structure that finally adopts the independent grouping mode is as follows:


 - index.php    Project entry file 
  - Common  Project Public Files Directory 
  - Conf  Project configuration directory 
  - Lang  Project language directory 
  - Modules  Independent grouping directory 
  The   - Home Home Grouping directory (refer to the previous section for independent grouping directory structure) 
  The   - Admin Admin Group directory 
  The   Off- ...  Other grouping directories 
  - Lib  Grouping Common Class Library Directory 
  The   - Action  Public Action Class library directory 
  The   - Behavior  Common Behavior Class Library Directory 
  The   - Model  Common model class library directory 
  The   Off- ...  Other public class library directories 
  - Runtime  Project runtime directory 
  The   - Cache  Template cache directory 
  The   - Data  Data cache directory 
  The   - Logs  Log file directory 
  The   Off- Temp  Temporary cache directory 


4. Template files

The independently grouped template files have been moved from the Tpl directory of the project to the Tpl directory of the independently grouped directory, and the original template grouping subdirectory is no longer needed, for example:


Tpl/Home/Index/index.html 

After moving to the Tpl directory under the standalone grouping, it should be:


Tpl/Index/index.html

Template theme functionality is still supported.

5. Call the class library

Independent grouping When importing class libraries, use methods and import project class libraries to basically 1, for example:


import('@.Action.TestAction'); //  Under the current grouping Action/TestAction.class.php
 import('@.ORG.Util.Image'); //  Under the current grouping ORG/Util/Image.class.php

Independent grouping does not consider the interaction and call between multiple groupings, and can only call the common class library.
If you have to call other grouped class libraries instead of using the common class library design, you can use:


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

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


Related articles: