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

How to solve the problem of thinkphp routing not converting

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-05-27 17:07:22
forward
1468 people have browsed it

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]',
];
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
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
Latest Issues
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
ThinkPHP Why use composer?
From 1970-01-01 08:00:00
0
0
0
thinkphp versions supported by php6
From 1970-01-01 08:00:00
0
0
0
thinkphp upload files
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template