Home >PHP Framework >ThinkPHP >How thinkphp intercepts routing

How thinkphp intercepts routing

PHPz
PHPzforward
2023-05-29 16:35:581571browse

1. What is route interception

Route interception is used to manage and control access paths to maintain the security and stability of the website. In order to prevent security issues and attacks, we usually filter and ban access paths. Through route interception, you can control accessed URLs and filter some invalid URLs, thereby improving the security and reliability of the website.

2. ThinkPHP route interception implementation method

The following is the ThinkPHP method to implement route interception:

1. Through the application configuration file config.php Configure routing rules;
2. By intercepting and judging before the controller is called;
3. By intercepting and judging before routing.

The following is a detailed introduction to the use of these three methods:

1. By configuring routing rules in the application configuration file config.php

In the application configuration file config.php Configuring routing rules is a feature in ThinkPHP. In the config.php file, you can find a configuration item named ‘URL_ROUTE_RULES’, in which routing rules can be defined. The specific operations are as follows:

return [
    'URL_ROUTE_RULES' => [
        'login'             => 'Index/login',
        'register'          => 'Index/register',
        'user/:id'          => 'User/index',
        'user/add'          => 'User/add',
        'user/edit/:id'     => 'User/edit',
        'user/delete/:id'   => 'User/delete',
    ],
];

2. By intercepting and judging before calling the controller

Using the controller extension function can implement routing interception in ThinkPHP. We can use the before method to intercept access requests and process them during the controller's expansion function. The following conditions need to be met to use the before method:

1. The controller needs to inherit the \think\Controller class;
2. The before method needs to return a bool type value, true means the interception is successful, false means the interception failed. .

The specific operations are as follows:

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    protected function before()
    {
        if(request()->action() == "index"){
            if(!session('loginTime')){
                return false;
            }
        }
        return true;
    }

    public function index()
    {
        return "hello world";
    }

    public function login()
    {
        return $this->fetch();
    }
}

3. By intercepting and judging before routing

We can use the routing extension function to implement routing interception in ThinkPHP. By using the before method, we can intercept and process access requests, thus extending the routing functionality. The following conditions need to be met to use the before method:

1. The route needs to define a closure function;
2. The before method needs to return a bool type value, true means the interception is successful, false means the interception failed.

The specific operations are as follows:

use think\Route;

Route::rule('/', function () {
    return 'hello world!';
}, 'GET')->before(function () {

    if(!session('loginTime')){
        return false;
    }
    return true;
});

3. Common routing errors and solutions

1. The requested method is not allowed

Cause of error: The request method is incorrect, such as using a get request to access the post route.

Solution: Check whether the route definition and request method are consistent.

2. Method definition not found

Cause of error: The request path does not match the method, or the routing rule definition is wrong.

Solution: Check whether the routing rules and the defined method names are consistent.

3. The controller does not exist

Cause of error: The corresponding controller cannot be found.

Solution: Check whether the controller class name and file name are consistent and in the correct location.

4. Missing parameters

Cause of error: Necessary parameters are missing in the request path.

Solution: Check whether the routing rules are correctly defined and pass the correct parameters.

5. "Access Denied" error

Cause of error: Insufficient permissions, or you have logged out.

Solution: Check information such as permission settings and login status.

Note: The above errors are only common errors, and specific errors must be investigated according to specific circumstances.

The above is the detailed content of How thinkphp intercepts routing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete