How to define routing in yii framework
Routing and URL generation
When a YII application starts processing a request, what it does first It is to convert the requested URL into a route. The role of routing is to subsequently instantiate corresponding controllers and operations in order to process requests. The entire processing process is called routing. (Recommended learning: yii framework)
The reverse process of routing is called URL generation, which refers to using the given routing and parameter information to generate a URL. When the generated URL is used to make a request, the routing processing process can parse it again and restore the original routing and parameter information.
The URL manager is mainly responsible for routing and URL generation, which is registered as an application component. The URL manager provides the method parseRequest() to parse the request and parse out the routing and parameter information.
The method createUrl() is used to generate a URL from the given route and parameter information. By configuring the URL manager in the application configuration, you can enable your application to recognize any URL format without modifying existing program code. For example, you can use the following code to generate a URL.
use yii\helpers\Url; // Url::to() calls UrlManager::createUrl() to create a URL $url = Url::to(['post/view', 'id' => 100]);
Depending on the configuration of the URL manager, the URL generated by the above code looks like the following. If this URL is subsequently requested, it will be parsed into the original route and parameter information above.
/index.php?r=post/view&id=100 /index.php/post/100 /posts/100
URL format
URL manager supports two URL formats: default URL format and pretty URL format. The default URL format uses a query parameter r to pass the route, and other parameters are placed in the URL in the normal way.
For example, the URL /index.php?r=post/view&id=100 has a route of post/view and a parameter id of 100. The default URL format does not require any configuration of the URL manager.
The beautiful URL format uses an additional path after the entry script name to display the route and related parameters.
For example, the additional path of URL /index.php/post/100 is /post/100, the route displayed is post/view and the parameter id is 100.
If you want to use this URL format, you need to design a URL rule set based on actual needs. You can switch between these two URL formats by modifying the value of the attribute enablePrettyUrl in the URL manager.
Routing
The work of routing can be divided into two steps:
1. Parse a route and Relevant parameters;
2. The controller operation that generates the response based on the route is used to process the request.
When using the default URL format, parsing out the route is very simple, just get the value of the parameter r;
When using the beautiful URL format, the URL manager will detect the URL rules set to find the route that matches the request. If no matching rule is found, an exception yii\web\NotFoundHttpException will be thrown.
Once the route has been parsed from the request, the next thing to do is to create the controller action associated with that route. Routes are slash-cut into several parts, for example, site/index will be cut into site and index.
Each part is an ID, they may point to a module, controller or operation. Starting from the first part of the route, the following steps should be performed to create the module (if any), controller and action:
1. Set the application body to the current module.
2. Check whether the current module's yii\base\Module::controllerMap contains the current ID. If so, a controller object will be created based on the configuration in the table, and then jump to step five to execute subsequent fragments of the route.
3. Check whether the ID points to a module in the module list in the yii\base\Module::modules attribute of the current module. If so, a module object will be created based on the configuration in the module table, and then the newly created module will be used as the environment to jump back to step two to parse the next route.
4. Treat this ID as a controller ID and create a controller object. Use the next step to parse the remaining fragments in the route.
5. The controller will search for the current ID in its yii\base\Controller::actions(). If it is found, it will create an action object based on the configuration in the mapping table; otherwise, the controller will try to create an inline action corresponding to the ID and defined by an action method.
If any errors occur in the above steps, the application will throw an exception yii\web\NotFoundHttpException, which means that routing processing failed.
The above is the detailed content of How to define routing in yii framework. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

If you're asking "What is Yii?" check out my previous tutorial: Introduction to the Yii Framework, which reviews the benefits of Yii and outlines what's new in Yii 2.0, released in October 2014. Hmm> In this Programming with Yii2 series, I will guide readers in using the Yii2PHP framework. In today's tutorial, I will share with you how to leverage Yii's console functionality to run cron jobs. In the past, I've used wget - a web-accessible URL - in a cron job to run my background tasks. This raises security concerns and has some performance issues. While I discussed some ways to mitigate the risk in our Security for Startup series, I had hoped to transition to console-driven commands

With the rapid development of the Internet, APIs have become an important way to exchange data between various applications. Therefore, it has become increasingly important to develop an API framework that is easy to maintain, efficient, and stable. When choosing an API framework, Yii2 and Symfony are two popular choices among developers. So, which one is more suitable for API development? This article will compare these two frameworks and give some conclusions. 1. Basic introduction Yii2 and Symfony are mature PHP frameworks with corresponding extensions that can be used to develop

In modern software development, building a powerful content management system (CMS) is not an easy task. Not only do developers need to have extensive skills and experience, but they also need to use the most advanced technologies and tools to optimize their functionality and performance. This article introduces how to use Yii2 and GrapeJS, two popular open source software, to implement back-end CMS and front-end visual editing. Yii2 is a popular PHPWeb framework that provides rich tools and components to quickly build
