Home > PHP Framework > YII > body text

Routing in Yii framework: implementing URL beautification

王林
Release: 2023-06-21 15:33:20
Original
814 people have browsed it

Yii framework is an open source high-performance web application development framework. It is based on the MVC design pattern and can help developers quickly build scalable web applications. In the Yii framework, routing is a very important concept. The role of routing is to map the URL requested from the client to the corresponding controller and action.

In traditional web applications, the URL is usually a string of characters with parameters, such as: http://www.example.com/index.php?id=1001&category=book. Such a URL makes it difficult for users to intuitively understand which page they are currently visiting, and it is not beautiful at the same time. In order to make it easier for users to access pages, the Yii framework provides a routing function that can convert URLs into a simpler and easier-to-understand form.

First, we need to configure routing rules in the application configuration file. The Yii framework provides three different routing methods: rule routing, enhanced routing and regular routing. Here we take rule routing as an example.

Rule routing is the most commonly used routing method, which maps a certain URL rule to a specified controller and action. Here is a simple example:

return [
    'components' => [
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'post/<id:d+>' => 'post/view',
                'posts' => 'post/index',
            ],
        ],
    ],
];
Copy after login

In the above example, we map the URL /post/1001 to the controller post and action view, where 1001 is a dynamically passed parameter. In this way, users can view the blog post with ID 1001 by accessing /post/1001. Additionally, we map the URL /posts to the controller post and action index so that users can view a list of blog posts by accessing /posts.

In addition to the fixed rules above, you can also use regular expressions to match URLs. For example:

return [
    'components' => [
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                [
                    'pattern' => '/post/<id:d+>',
                    'route' => 'post/view',
                    'suffix' => '.html',
                ],
                [
                    'pattern' => '/<category:w+>',
                    'route' => 'post/index',
                    'suffix' => '.html',
                ],
            ],
        ],
    ],
];
Copy after login

In the above example, we used two regular expressions to match URLs. The first rule maps /post/1001.html to the controller post and action view, where .html is the suffix and is the dynamically passed in parameter. The second rule maps /news.html or /technology.html to the controller post and action index, where is a dynamically passed parameter that can match any letter.

In addition to rule routing, the Yii framework also provides enhanced routing and regular routing. Enhanced routing is similar to rule routing and can map URLs to specified controllers and actions. The difference is that enhanced routing supports automatic resolution of the names of modules, controllers and actions, and can automatically populate parameters into specified model objects.

Regular routing is a more powerful routing method that can use regular expressions to match any URL. Regular routing has relatively few usage scenarios and is generally used to match special URL formats.

In general, using routing in the Yii framework can easily beautify the URL and make it easier for users to access the page. At the same time, routing is also an important part of building MVC applications, and different routing methods can meet different needs. When developing web applications, we need to choose the most appropriate routing method based on specific circumstances to improve application performance and user experience.

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

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!