Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How YII uses url components to beautify management

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how YII uses url components to beautify management. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The details are as follows:

UrlManager component

The official documentation of yii explains this as follows:

UrlSuffix the url suffix used by this rule, defaults to CurlManger::urlSuffix, and the value is null. For example, you can set this to .html to make url look like a static page.

Whether caseSensitive is case-sensitive, CUrlManager::caseSensitive is used by default, and the value is null.

DefaultParams the default get parameter used by this rule. When you use this rule to parse a request, the value of this parameter is injected into the $_ GET parameter.

MatchValue when creating a URL, whether the GET parameter matches the corresponding subpattern. CurlManager::matchValue is used by default, and the value is null.

If the attribute is false, it means that a URL will be created when the route and parameter name match a given rule.

If the property is true, then the given parameter value night must match the corresponding parameter subpattern.

Note: setting this property to true degrades performance.

We use some examples to explain the working rules of web sites. Let's assume that our rules include the following three:

Array ('posts'= >' post/list', 'post/'= >' post/read', 'post//'= >' post/read',)

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 sample post' title = >'a sample post')) to generate / index.php/post/2008/a%20sample%20post. 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 the 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, and if the rule of the route 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 recorded yearbook = > 2008)), we will get / index.php/post/100?year=2008. In order for these extra parameters to appear as part of the path information, we should append / * to the rule. So, with the rule post//*, we can get the URL / index.php/post/100/year/2008.

As we mentioned, the other use of the URL rule is to parse the request URL. Of course, this is an inverse process of URL generation. For example, when a user requests / index.php/post/100, the second rule in the above example will apply to resolve the route post/read and GET parameter array ('id'= > 100) (available through $_ GET).

Tip: what this URL produces through the createurl method is a relative address. To get an absolute url, we can use the prefix yii:: app ()-> hostInfo, or call createAbsoluteUrl.

Note: the URL rules used will degrade the performance of the application. This is because when parsing the requested URL, [CUrlManager] tries to match it with each rule until a rule is applicable. Therefore, high-traffic website applications should minimize the use of URL rules.

Test.com/vthot wants to generate test.com/vthot/

The copy code is as follows:

'urlSuffix'= >'/'

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

'urlManager'= > array (' urlFormat'= > 'path',' showScriptName'= > false, 'urlSuffix'= >' .html', 'rules'= > array (' posts'= > 'post/list',' post/'= > array ('post/show','urlSuffix'= > .html'), 'post//'= > array (' post/view','urlSuffix'= > '.xml'),)

Example one

Rule code

The copy code is as follows:

'posts'= >' post/list'

Action code

The copy code is as follows:

Echo $this- > createAbsoluteUrl ('post/list')

Output

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

Example two

Rule code

The copy code is as follows:

'post/'= > array ('post/show','urlSuffix'= >' .html')

Action code

The copy code is as follows:

Echo $this- > createAbsoluteUrl ('post/show',array (' id'= > 998, 'name'= >' 123')

Output

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

Example three

Rule Code:

The copy code is as follows:

'post//'= > array ('post/view','urlSuffix'= >' .xml')

Action code

The copy code is as follows:

Echo $this- > createAbsoluteUrl ('post/view',array (' id'= > 998, 'mid'= >' tody'))

Output

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

Example 4

Rule code

The copy code is as follows:

'http://.vt.com/'=>array('/host','urlSuffix'=>'.me'),

Action Code:

Echo $this- > createAbsoluteUrl ('look/host',array (' user'= > 'boy','mid'= >' ny-01')); echo'; echo $this- > createAbsoluteUrl ('looks/host',array (' user'= > 'boy','mid'= >' ny-01'))

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

Public function actionUpdate () {$id = Yii::app ()-> request- > getQuery ('id'); processed $_ GET [' id']} / / $id = Yii::app ()-> request- > getPost ('id'); processed $_ POST [' id'] / / $id = Yii::app ()-> request- > getParam ('id'); / / CHttpRequest more

2) public function actionUpdate ($id), which does not support multiple primary keys, will check whether there is id in the GET. Access is not allowed without id.

'sayhello/' = >' post/hello', name is the parameter of PostController actionHello ($name) 'post/' = >' post/view', domain/post/e lowercase where the preceding alias is the parameter of PostController actionView ($alias)'(posts | archive) /'= > 'post/index', domain/posts/DESC or domain/posts/ASC' (posts | archive)' = > 'post/index', domain/posts or domain/archive'tos' = > array (' website/page') 'defaultParams' = > array (' alias' = > 'terms_of_service'))

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

Hide index.php

There is another thing we can do to further clean up our URL, that is, to hide the index.php entry script in URL. This requires us to configure the Web server as well as the urlManager application components.

1.add showScriptName= > false

2.add project/.htaccess

RewriteEngine on# if a directory or a file exists, use it directlyRewriteCond% {REQUEST_FILENAME}!-fRewriteCond% {REQUEST_FILENAME}!-d # otherwise forward it to index.phpRewriteRule. Index.php

3. Turn on rewrite

To put it simply, simply set up urlManager in main.php, and then talk about three rules, basically covering all of them. Finally, hide index.php, keeping in mind that .htaccess is in the index.php sibling directory, not the protected/ directory. The rest is simple.

On "YII how to use url components to beautify management" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report