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!

The Yii framework is suitable for developing web applications of all sizes, and its advantages lie in its high performance and rich feature set. 1) Yii adopts an MVC architecture, and its core components include ActiveRecord, Widget and Gii tools. 2) Through the request processing process, Yii efficiently handles HTTP requests. 3) Basic usage shows a simple example of creating controllers and views. 4) Advanced usage demonstrates the flexibility of database operations through ActiveRecord. 5) Debugging skills include using the debug toolbar and logging system. 6) Performance optimization It is recommended to use cache and database query optimization, follow coding specifications and dependency injection to improve code quality.

In Yii2, there are two main ways to display error prompts. One is to use Yii::$app->errorHandler->exception() to automatically catch and display errors when an exception occurs. The other is to use $this->addError(), which displays an error when model validation fails and can be accessed in the view through $model->getErrors(). In the view, you can use if ($errors = $model->getErrors())

With the continuous development of PHP framework technology, Yi2 and TP5 have attracted much attention as the two mainstream frameworks. They are all known for their outstanding performance, rich functionality and robustness, but they have some differences and advantages and disadvantages. Understanding these differences is crucial for developers to choose frameworks.

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

The Yi2 Rate Limiting Guide provides users with a comprehensive guide to how to control the data transfer rate in Yi2 applications. By implementing rate limits, users can optimize application performance, prevent excessive bandwidth consumption and ensure stable and reliable connections. This guide will introduce step-by-step how to configure the rate limit settings of Yi2, covering a variety of platforms and scenarios to meet the different needs of users.

Article Summary: Yii Framework is an efficient and flexible PHP framework for creating dynamic and scalable web applications. It is known for its high performance, lightweight and easy to use features. This article will provide a comprehensive tutorial on the Yii framework, covering everything from installation to configuration to development of applications. This guide is designed to help beginners and experienced developers take advantage of the power of Yii to build reliable and maintainable web solutions.

This article introduces the latest tutorial on calling public functions, which is implemented in Easy Language (Yi) language. For beginners, easy-to-language programming languages are easy to learn, and this article provides a detailed step-by-step guide to help users master how to call public functions in Yi applications. By following this tutorial, users will learn how to define, load, and call common functions, thereby enhancing their code reusability and flexibility.

Yii2 is a powerful PHP framework that has been widely praised by developers. With its high performance, scalability and user-friendly interface, it becomes ideal for building large, complex web applications. However, like any framework, Yii2 has some advantages and disadvantages to consider.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment