Home > PHP Framework > YII > Routing configuration in Yii framework: URL beautification

Routing configuration in Yii framework: URL beautification

WBOY
Release: 2023-06-21 15:59:00
Original
1259 people have browsed it

With the rapid development of the Internet, people's experience in using websites is getting better and better. Among them, the form of URL has gradually become an important factor for users to judge the quality of the website. For URL beautification, the Yii framework provides a variety of routing configuration solutions. This article will focus on routing configuration in the Yii framework and how to implement URL beautification.

1. Basics of routing configuration

Routing refers to mapping the requested URL address to specific controllers and methods to achieve specific processing of the request. In the Yii framework, routing configuration is completed through the URL manager (UrlManager). The URL manager is in the application's configuration file config folder, as follows:

'components' => [
    'urlManager' => [
        'class' => 'yiiwebUrlManager',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            // ...
        ],
    ],
],
Copy after login

Among them, enablePrettyUrl indicates whether to enable beautified URLs, and it is recommended to set it to true; showScriptName indicates whether to display the script file name (such as index.php) in the URL. It is also recommended to set it to false to avoid affecting the beauty of the URL.

2. Static routing

Static routing means that a specific URL address can be directly mapped to a controller and method. For example, we can statically convert a URL address such as index.php?r=site/about to about, and directly enter http://yourdomain.com/ when accessing Just about.

In the Yii framework, the method to implement static routing is as follows:

'rules' => [
    'about' => 'site/about',
],
Copy after login

Here about represents the mapped URL address, site/about is Represents specific controllers and methods.

3. Dynamic routing

Dynamic routing refers to mapping URL addresses with parameters to controllers and methods, and passing data through parameters. For example, we can dynamically convert a URL address such as index.php?r=site/view&id=1 to view/1, and directly enter http:// when accessing. yourdomain.com/view/1 is enough.

In the Yii framework, the method to implement dynamic routing is as follows:

'rules' => [
    'view/<id:d+>' => 'site/view',
],
Copy after login

The view/<id:d > here represents the mapped URL address, whereid is the parameter name, d is a regular expression matching numbers, site/view represents the specific controller and method.

4. Advanced routing

Advanced routing is an extension of static and dynamic routing, which can achieve more complex URL mapping. For example, we can convert a URL address such as index.php?r=user/profile to profile/username, and directly enter http://yourdomain.com when accessing /profile/admin is enough.

In the Yii framework, the method to implement advanced routing is as follows:

'rules' => [
    [
        'class' => 'yiiwebUrlRule',
        'pattern' => 'profile/<username:w+>',
        'route' => 'user/profile',
        'suffix' => '',
    ],
],
Copy after login

The 'class' => 'yiiwebUrlRule' means that we are using advanced routing. 'pattern' => 'profile/<username:w >' represents the matching rule of the URL address, where <username:w > represents the parameter name and regular expression , 'route' => 'user/profile' represents the specific controller and method, 'suffix' => '' represents the additional characters at the end of the URL address.

Summary

Through the above introduction, we can understand the basic methods of routing configuration in the Yii framework and the differences therein. However, when configuring routing, you need to pay attention to the beauty and legibility of the URL address, and also make good arrangements for the underlying controllers and methods. Only by achieving a balance between the two aspects can the URL address of the website be better mapped to specific controllers and methods in different scenarios, thereby improving the website experience.

The above is the detailed content of Routing configuration in Yii framework: URL beautification. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template