Home >PHP Framework >ThinkPHP >How to solve the problem of thinkphp routing not converting

How to solve the problem of thinkphp routing not converting

WBOY
WBOYforward
2023-05-27 17:07:221427browse

1. Problem description

When we use ThinkPHP for development, sometimes we encounter such a situation: when we perform some routing operations, the page It does not jump to the interface we expected.

For example, we hope to access the "do" method in "HomeController.php" through the URL "www.example.com/home/do", but we actually cannot access this method. At the same time, we found that no error message appeared, which made us unable to start.

2. Cause analysis

In ThinkPHP, routing is matched through URL suffix. If we do not use any suffix in our URL, then ThinkPHP will think that the method we want to access is the default method in the controller (the default method name is generally "index").

For example, when we use the URL "www.example.com/home/index", we will access the "index" method in "HomeController.php" by default.

When we use the URL "www.example.com/home/do", because no suffix is ​​used, ThinkPHP will think that we want to access the "index" method, and because the "do" method It does not exist in the controller, so page access will fail.

3. Solution

1. Use the "/" suffix

If you add a slash "/" at the end of the URL, you can avoid it. Route mismatch situation. For example, we can use the URL "www.example.com/home/do/" to access the "do" method in "HomeController.php".

At the same time, when we use ThinkPHP for development, it is recommended to set the URL suffix to "/" in the configuration file config.php, so as to avoid the problem of routes not being converted.

2. Modify routing rules

We can also solve the problem by modifying routing rules.

First, in our routing configuration file (usually route.php), we can define the routing rules as:

return [
    'home/do/[:id]' => 'home/[:action]',
];

In this way, we can pass the URL "www.example. com/home/do/1" to access the "do" method in "HomeController.php" and pass an id parameter in the URL.

The above is the detailed content of How to solve the problem of thinkphp routing not converting. 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