How YII uses url components to beautify management

不言
Release: 2023-04-01 10:04:02
Original
1410 people have browsed it

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:

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/

&#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:

&#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

&#39;posts&#39;=>&#39;post/list&#39;,
Copy after login

Action code

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

Output

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

Example 2

Rule code

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

Action code

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:

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

Action code

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

&#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:

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

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

&#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

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!

Related labels:
yii
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!