Home >PHP Framework >ThinkPHP >How to use friendly URL addresses in ThinkPHP6?

How to use friendly URL addresses in ThinkPHP6?

WBOY
WBOYOriginal
2023-06-12 08:52:541354browse

With the development of the Internet, more and more websites need to consider optimizing user experience, one of which is friendly URL addresses. ThinkPHP is an excellent PHP framework that also provides a convenient solution for processing URL addresses. This article will introduce how to use friendly URL addresses in ThinkPHP6.

First of all, we need to understand the related concepts of routing in ThinkPHP6. Routing refers to forwarding URL requests to specified controllers and methods to implement different business logic. ThinkPHP6 can define routing information through controller annotations, which is more convenient during the maintenance process.

For URL addresses, we commonly have the following two forms:

1. Traditional URL addresses such as: http://www.example.com/index.php/Controller/Action /id/1
2. Pseudo-static URL address such as: http://www.example.com/Controller/Action/id/1

In the first traditional URL address, we need Manually adding controller and method names in the URL address and passing data in the form of id parameters may lead to code confusion and difficulty in maintaining if you are not careful. In the second type of pseudo-static URL address, we can convert parameters such as ID into a URL address that is highly readable and easy to maintain.

Next, we introduce how to use routing to implement pseudo-static URL addresses.

1. Configure routing
The config/route.php file is a routing file, and we can configure routing information in this file. The following is a simple configuration example:

use thinkacadeRoute;

Route::rule('article/:id', 'index/article/read')
        ->pattern(['id' => 'd+']);

In the above code, we configured a named article/:id through the Route::rule() method URL address, where the :id part is the URL parameter, which represents the data that needs to be passed. The actual operation of routing is to forward this URL address to a controller and method named index/article/read.

At the same time, we can use the ->pattern() method to limit the parameter format. In the above example, we restricted the value of the :id parameter to only be numbers.

2. Define the controller
After defining the routing information, we need to implement the corresponding method in the controller to process the request and return the content. In the controller file (located in the app/controller directory), we can define a controller class named article and a method named read.

namespace appcontroller;

use thinkRequest;

class Article
{
    public function read($id)
    {
        return '正在阅读文章 ' . $id;
    }
}

3. Access URL
Now, we can access our website through the address bar, for example, enter the following URL address: http://www.example.com/article/1, corresponding to the controller The /:id parameter value is 1, thus entering the program processing flow. After completing the route forwarding, we pass the request to the read method of the Article controller to process the request. After completing the processing, we display the returned content in the front-end page and implement a friendly URL address.

This article introduces the specific method of implementing friendly URL addresses in ThinkPHP6. Using routing can effectively avoid cumbersome URL address input and the passing of a large number of parameters, making the code easier to maintain and improving user experience.

The above is the detailed content of How to use friendly URL addresses in ThinkPHP6?. 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