Home  >  Article  >  PHP Framework  >  Routing in Yii framework: implementing URL beautification

Routing in Yii framework: implementing URL beautification

王林
王林Original
2023-06-21 15:33:20725browse

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

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

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 1ae845aa4984539391bc1a58ac88f77f is the dynamically passed in parameter. The second rule maps /news.html or /technology.html to the controller post and action index, where 5d6232a63e0687f3861341750f3c1b35 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!

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