Home >PHP Framework >ThinkPHP >How to add a new module in ThinkPHP (steps)

How to add a new module in ThinkPHP (steps)

PHPz
PHPzOriginal
2023-04-07 09:32:081408browse

ThinkPHP is a PHP open source framework using MVC pattern architecture, which is very suitable for quickly developing modern web applications. In practical applications, we often need to add new modules to meet business needs. Next, I will introduce how to add new modules in ThinkPHP.

1. How to add a new module

In ThinkPHP, adding a new module is actually very simple. It only requires two steps:

Step 1: Create a new file Folder

In the ThinkPHP application directory, find the folder named "Home", copy it and rename it to the name of the module you want to add. For example, you want to add a new module named "Admin" " module, copy the "Home" folder and name it "Admin".

Step 2: Configure routing

In the newly added module directory, find the folder named "Conf", open the "config.php" file, and add a new routing rule. For example:

'admin/:controller/:action' => 'Admin/:controller/:action',

This routing rule means that when you access "admin/controller name/method name", you actually access the controller and method under the Admin module.

2. How to create controllers and views

Creating controllers and views is also very simple.

  1. Create Controller

In the newly added module directory, find the folder named "Controller" and create a controller file. For example, if you want to create a For a controller named "User", you can create a file named "UserController.class.php" under the "Controller" folder and write the controller code in the file.

namespace Admin\Controller;
use Think\Controller;

class UserController extends Controller {
    public function index() {
        $this->display();
    }
}
  1. Create View

Under the "View" folder, create a folder named "User" and create a folder named "index. html" file.

<!DOCTYPE html>
<html>
<head>
    <title>用户列表</title>
</head>
<body>
    <h1>用户列表</h1>
</body>
</html>

The above are the basic steps for adding new modules, creating controllers and views in ThinkPHP.

3. How to access the newly added module

After you complete the above steps, you can access the newly added module and its controller and view. For example, when you access "admin/user/index", you actually access the index method in the UserController controller under the Admin module, and display the user list in the view.

4. Summary

It is very simple to add a new module in ThinkPHP. It only requires two steps: create a new folder and configure routing. At the same time, it is very simple to create controllers and views in newly added modules. By studying this article, you can easily add new modules to ThinkPHP to quickly meet business needs.

The above is the detailed content of How to add a new module in ThinkPHP (steps). For more information, please follow other related articles on the PHP Chinese website!

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