Home Backend Development PHP Tutorial How YII uses url components to beautify management

How YII uses url components to beautify management

Jun 15, 2018 pm 02:03 PM
yii

This article mainly introduces how YII uses the url component to beautify management. It analyzes the specific functions and related usage skills of the urlManager component in detail in the form of examples. Friends in need can refer to the examples in this article.

Describes how YII uses url components to beautify management. Share it with everyone for your reference, the details are as follows:

urlManager component

yii’s official documentation explains this as follows:

urlSuffix The url suffix used by this rule, CurlManger::urlSuffix is ​​used by default, and the value is null. For example, you can set this to .html to make the url look "like" a static page.
caseSensitive Whether it is case sensitive, CUrlManager::caseSensitive is used by default, and the value is null.
defaultParams The default get parameters used by this rule. When using this rule to parse a request, the value of this parameter will be injected into the $_GET parameter.
matchValue When creating a URL, whether the GET parameters match the corresponding sub-pattern. By default, CurlManager::matchValue is used, and the value is null.

If this attribute is false, it means that when the route and parameter names match the given rules, a URL will be created accordingly.

If this attribute is true, then the given parameter value must match the corresponding parameter sub-pattern.

Note: Setting this property to true will reduce performance.

We use some examples to explain the URL working rules. We assume that our rules include the following three:

1

2

3

4

5

array(

  'posts'=>'post/list',

  &#39;post/<id:\d+>&#39;=>&#39;post/read&#39;,

  &#39;post/<year:\d{4}>/<title>&#39;=>&#39;post/read&#39;,

)

Copy after login

Call $this->createUrl('post/list') to generate /index.php/posts . The first rule applies.

Call $this->createUrl('post/read',array('id'=>100)) to generate /index.php/post/100. The second rule applies.

Call $this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post')) to generate /index.php/post /2008/a sample post. The third rule applies.

Call $this->createUrl('post/read') to generate /index.php/post/read. Please note that no rules apply.

In summary, when using createUrl to generate a URL, the route and GET parameters passed to the method are used to determine which URL rules apply. If each parameter in the association rule can be found in the GET parameter, it will be passed to createUrl. If the route rule also matches the route parameter, the rule will be used to generate the URL.

If the GET parameter passed to createUrl is one of the rules required above, the other parameters will appear in the query string. For example, if we call $this->createUrl('post/read',array('id'=>100,'year'=>2008)) , we will get /index.php/post/100? year=2008. To make these extra parameters appear as part of the path information, we should append /* to the rule. Therefore, with the rule post//* , we can get the URL /index.php/post/100/year/2008 .

As we mentioned, other uses of URL rules are to parse request URLs. Of course, this is a reverse process of URL generation. For example, when the user requests /index.php/post/100, the second rule of the above example will be applied to parse the route post/read and the GET parameter array('id'=>100) (available via $_GET).

Tip: This URL is a relative address generated by the createurl method. In order to get an absolute url, we can use the prefix yii: :app()->hostInfo, or call createAbsoluteUrl.

Note: The URL rules used will reduce the performance of the application. This is because when parsing a requested URL, [CUrlManager] tries to match it using each rule until a certain rule can apply. Therefore, high-traffic website applications should minimize the URL rules they use.

test.com/vthot Want to generate test.com/vthot/

1

&#39;urlSuffix&#39;=>&#39;/&#39;,

Copy after login

To change the URL format, we should configure the urlManager application element so that createUrl can automatically switch to the new format and the application can Correctly understand the new URL:

1

2

3

4

5

6

7

8

9

10

&#39;urlManager&#39;=>array(

  &#39;urlFormat&#39;=>&#39;path&#39;,

  &#39;showScriptName&#39;=>false,

  &#39;urlSuffix&#39;=>&#39;.html&#39;,

  &#39;rules&#39;=>array(

    &#39;posts&#39;=>&#39;post/list&#39;,

    &#39;post/<id:\d+>&#39;=>array(&#39;post/show&#39;,&#39;urlSuffix&#39;=>&#39;.html&#39;),

    &#39;post/<id:\d+>/<mid:\w+>&#39;=>array(&#39;post/view&#39;,&#39;urlSuffix&#39;=>&#39;.xml&#39;),

  ),

),

Copy after login

Example 1

Rule code

1

&#39;posts&#39;=>&#39;post/list&#39;,

Copy after login

Action code

1

echo $this->createAbsoluteUrl(&#39;post/list&#39;);

Copy after login

Output

http://localhost/test/index.php/post

Example 2

Rule code

1

&#39;post/<id:\d+>&#39;=>array(&#39;post/show&#39;,&#39;urlSuffix&#39;=>&#39;.html&#39;),

Copy after login

Action code

1

echo $this->createAbsoluteUrl(&#39;post/show&#39;,array(&#39;id&#39;=>998, &#39;name&#39;=>&#39;123&#39;));

Copy after login

Output

http://localhost/test/index.php/post/998.html?name=123

Example 3

Rule code:

1

&#39;post/<id:\d+>/<mid:\w+>&#39;=>array(&#39;post/view&#39;,&#39;urlSuffix&#39;=>&#39;.xml&#39;),

Copy after login

Action code

1

echo $this->createAbsoluteUrl(&#39;post/view&#39;,array(&#39;id&#39;=>998, &#39;mid&#39;=>&#39;tody&#39;));

Copy after login

Output

http://localhost/test/index.php/post/998/tody.xml

Example Four

Rule code

1

&#39;http://<user:\w+>.vt.com/<_c:(look|seek)>&#39;=>array(&#39;<_c>/host&#39;,&#39;urlSuffix&#39;=>&#39;.me&#39;),

Copy after login

Action code:

1

2

3

echo $this->createAbsoluteUrl(&#39;look/host&#39;,array(&#39;user&#39;=>&#39;boy&#39;,&#39;mid&#39;=>&#39;ny-01&#39;));

echo &#39;&#39;;

echo $this->createAbsoluteUrl(&#39;looks/host&#39;,array(&#39;user&#39;=>&#39;boy&#39;,&#39;mid&#39;=>&#39;ny-01&#39;));

Copy after login

Output

http://boy .vt.com/look.me?mid=ny-01
http://localhost/test/index.php/looks/host/user/boy/mid/ny-01

1) controller/Update/id/23

1

2

3

4

5

public function actionUpdate(){

  $id = Yii::app()->request->getQuery(&#39;id&#39;) ; 经过处理的$_GET[&#39;id&#39;]

}

//$id = Yii::app()->request->getPost(&#39;id&#39;); 经过处理的$_POST[&#39;id&#39;]

//$id = Yii::app()->request->getParam(&#39;id&#39;); //CHttpRequest更多

Copy after login

2) public function actionUpdate($id) This does not support multiple primary keys, it will check whether there is one in GET id, access is not allowed directly without id

1

2

3

4

5

&#39;sayhello/<name>&#39; => &#39;post/hello&#39;, name是PostController actionHello($name)的参数

&#39;post/<alias:[-a-z]+>&#39; => &#39;post/view&#39;,  domain/post/e文小写 其中:前面的alias是PostController actionView($alias)的参数

&#39;(posts|archive)/<order:(DESC|ASC)>&#39; => &#39;post/index&#39;, domain/posts/DESC或domain/posts/ASC

&#39;(posts|archive)&#39; => &#39;post/index&#39;, domain/posts或domain/archive

&#39;tos&#39; => array(&#39;website/page&#39;, &#39;defaultParams&#39; => array(&#39;alias&#39; =>&#39;terms_of_service&#39;)),

Copy after login

When the URL is /tos, pass terms_of_service as the alias parameter value.

隐藏 index.php

还有一点,我们可以做进一步清理我们的网址,即在URL中藏匿index.php 入口脚本。这就要求我们配置Web服务器,以及urlManager应用程序元件。

1.add showScriptName=>false

2.add project/.htaccess

1

2

3

4

5

6

RewriteEngine on

# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php

RewriteRule . index.php

Copy after login

3.开启rewrite

简单的说,在main.php中简单设置urlManager,然后讲了3条规则,基本都覆盖到了。最后是隐藏index.php,请记住.htaccess位于index.php同级目录 ,而不是protected/目录。其他就简单了。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

关于Yii基于数组和对象的Model查询

Yii和CKEditor实现图片上传的功能

Yii2如何使用Bootbox插件实现自定义弹窗

The above is the detailed content of How YII uses url components to beautify management. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

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

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

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.

Symfony vs Yii2: Which framework is better for developing large-scale web applications? Symfony vs Yii2: Which framework is better for developing large-scale web applications? Jun 19, 2023 am 10:57 AM

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

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

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

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

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

Yii2 Programming Guide: How to run Cron service Yii2 Programming Guide: How to run Cron service Sep 01, 2023 pm 11:21 PM

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

Yii2 vs Symfony: Which framework is better for API development? Yii2 vs Symfony: Which framework is better for API development? Jun 18, 2023 pm 11:00 PM

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

How to convert yii objects into arrays or directly output to json format How to convert yii objects into arrays or directly output to json format Jan 08, 2021 am 10:13 AM

Yii framework: This article introduces Yii's method of converting objects into arrays or directly outputting them into json format. It has certain reference value and I hope it can help you.

See all articles