Home > Article > PHP Framework > How to implement yii2 routing
Route
It is inevitable to use URLs in Web development. The most commonly used one is to generate a URL pointing to another page in the application. Developers need a concise, centralized, and unified way to complete this process.
Otherwise, writing a lot of code such as http://www.digpage.com/post/view/100 in the code will be too verbose and secondly It is error-prone and difficult to troubleshoot. Third, it is easy to make omissions when revising in the future. (Recommended study: yii tutorial)
Therefore, from a development perspective, a more concise solution that can be managed uniformly and can troubleshoot errors is needed.
At the same time, we explained how to configure the web server for Yii in the :ref:install section, from which we can find that all user requests are sent to the entry script index.php for processing.
So, developers need an efficient way to determine which controller and which action should be used to process the request.
Based on the above two requirements, Yii provides routing and URL management components.
The so-called route refers to the part of the URL used to identify the module, controller, and action used to process user requests. It is generally specified by the r query parameter.
For example, http://www.digpage.com/index.php?r=post/view&id=100, indicating that this request will be processed by PostController's actionView.
At the same time, Yii also provides a function to beautify URLs, so that the above URL can be expressed in a neater and more beautiful form, such as http://www.digpage.com/post/view/ 100. The implementation of this function relies on an application component called urlManager.
Using urlManager, developers can parse user requests and assign corresponding modules, controllers and actions for processing. They can also generate the required URLs and return them to users based on predefined routing rules.
In short, urlManger has two functions: parsing requests to determine who to assign to handle the requests and generating URLs based on routing rules.
The above is the detailed content of How to implement yii2 routing. For more information, please follow other related articles on the PHP Chinese website!