Current Location: Home> Latest Articles> How to Set Up and Manage Groups in ThinkPHP Framework

How to Set Up and Manage Groups in ThinkPHP Framework

gitbox 2025-06-28

How to Set Up and Manage Groups in ThinkPHP Framework

In the ThinkPHP framework, groups are a way to divide the application by functionality or module. Each group has its own set of controllers, models, and view files, improving code structure, maintainability, and scalability. This article will guide you on how to set up groups in ThinkPHP.

Creating a Group

Group Directory Structure

First, create a new group directory in the application directory of ThinkPHP. You can quickly create a group using the command-line tool:

php think build app [group_name]

After executing the command, a directory named [group_name] will be generated in the application directory. For example, executing “php think build app admin” will create a directory called “admin”.

Configuring Routes for the Group

In ThinkPHP, routes are used to map URL requests to corresponding controller methods. To configure routes for the newly created group, you need to modify the route.php file in the config directory of the application.

Open the route.php file, find the “Route::rule” method, and add the routing rules for the group. For example, the following code maps requests for the admin group to the Index controller's index method:

Route::rule('admin', 'admin/Index/index');

In this code, 'admin' is the group name, and 'admin/Index/index' is the controller and method path.

Group Directory Structure

After creating the group, you need to organize related files and directories under the group. A typical group directory structure is as follows:

admin (group directory) ├── controller (controller directory) ├── model (model directory) └── view (view directory)

In the controller directory, you can create different controller files, each responsible for different functional modules; in the model directory, create model files corresponding to the controllers to handle data; the view directory is used to store template files that display data.

Defining Controllers and Methods

Controllers are the core components for processing user requests and business logic. In the controller file, you can define multiple methods to handle different requests.

For example, create a controller file named Index.php and define a method named index to handle requests to the homepage. The following is a sample code:

namespace app\admin\controller;

class Index
{
    public function index()
    {
        // Logic to handle the index request
    }
}

Writing View Files

Under the group's view directory, you can create corresponding view files for the controllers to display data. For example, create a view file named index.html to display the content returned from the index method.

Content of the view file

Accessing the Group

After completing the above steps, you can access the group by visiting the specified URL. For example, if the configured group name is admin, you can access the group by visiting “http://localhost/admin”.

Conclusion

This article explains how to set up groups in ThinkPHP, including creating group directories, configuring routes, defining controllers and view files, etc. Group setup not only improves project organization and maintainability but also facilitates team development and expansion.

Keywords: group setup, ThinkPHP, controllers, models, views, routing