search
HomeBackend DevelopmentPHP TutorialDecrypt 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:

'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
What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

See all articles

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment