search
Homephp教程PHP开发Yii Framework Analysis (8) - URL Management Component

URL management component mainly provides 2 functions:
1. According to the URL entered by the user, parse out the route for processing this request - which Action of which Controller handles it, and at the same time add the parameter part in the URL to in the $_GET parameter. In every web framework, such a component is needed to perform route distribution work.
2. Create a url based on the route and parameter array. The URL can be hard-coded in the view layer, that is, the URL address is written directly, but this often lacks flexibility and brings costs to later maintenance.

array(
    'components'=>array(
        'urlFormat'=>'path',
        'rules'=>array(
            &#39;/art/<cate:\w+>/<key:\d+>/<id:\d+>/<p:\d+>&#39;=>&#39;article/<cate>/<key>&#39;,
            &#39;post/<id:\d+>/<title:.*?>&#39;=>&#39;post/view&#39;,
            &#39;<controller:\w+>/&#39;=>&#39;<controller>/&#39;,
         ),
    ),
);</action></controller></action:\w+></controller:\w+></title:.*?></id:\d+></key></cate></p:\d+></id:\d+></key:\d+></cate:\w+>

The above is the configuration of a url management component, with a total of 3 rules. The figure below takes the first rule as an example to illustrate the two functions of url parsing and url creation. For each routing rule, CUrlManager will create a CUrlRule object to handle the two functions corresponding to this rule, so if there are several rules, there will be several CUrlRule objects. So CUrlRule is the core of url management. Let's analyze the working principle of CUrlRule.

Yii Framework Analysis (8) - URL Management Component

Each url routing rule is processed by a CUrlRule object. Next, take the following routing rule as an example: '/art////'=>'article //', describes the processing process of url parsing and url creation. The process of processing URLs by each CUrlRule object can be divided into 3 stages:
1. Initializing the CUrlRule object
In the constructor of the CUrlRule object, 6 important member variables will be initialized:

Yii Framework Analysis (8) - URL Management Component

2. Parse url
The work of parsing url is divided into three steps: a. Parse out each field in the url according to the pattern rules; b. Replace the reference fields in the route according to references. ;c. Add the fields specified in params to the $_GET array
3. Create url
The work of creating url is divided into three steps: a. According to the routePattern rules, parse out each field in the input route; b , merge the input parameter array and the array parsed in the previous step; c. Replace the template with the merged array.

Yii Framework Analysis (8) - URL Management Component

The above is the Yii framework analysis (8) - the content of the URL management component. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools