Home >Backend Development >PHP Tutorial >YII uses the url component to beautify and manage the method, yiiurl component beautification_PHP tutorial

YII uses the url component to beautify and manage the method, yiiurl component beautification_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:22984browse

YII uses the url component to beautify and manage, yiiurl component beautifies

This article describes the example of YII using the url component to beautify and manage. 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. By default, CurlManger::urlSuffix is ​​used, 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, the default is CUrlManager::caseSensitive, 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 how URLs work. Let's assume that our rules include the following three:

array(
  'posts'=>'post/list',
  'post/<id:\d+>'=>'post/read',
  'post/<year:\d{4}>/<title>'=>'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,'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. So, with the rule post/1ae845aa4984539391bc1a58ac88f77f/* , 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 the 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 wants to generate test.com/vthot/
Copy code The code is as follows: 'urlSuffix'=>'/',
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:

'urlManager'=>array(
  'urlFormat'=>'path',
  'showScriptName'=>false,
  'urlSuffix'=>'.html',
  'rules'=>array(
    'posts'=>'post/list',
    'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),
    'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),
  ),
),

Example 1

Rule code
Copy code The code is as follows: 'posts'=>'post/list',
Action code
Copy code The code is as follows: echo $this->createAbsoluteUrl('post/list');

Output

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

Example 2

Rule code
Copy code The code is as follows: 'post/1ae845aa4984539391bc1a58ac88f77f'=>array('post/show','urlSuffix'=>'.html') ,
Action code
Copy code The 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 3

Rule code:
Copy code The code is as follows: 'post/1ae845aa4984539391bc1a58ac88f77f/88c146a9df84b1a6da6e489e2eadbfe5'=>array('post/view','urlSuffix' =>'.xml'),

Action代码
复制代码 代码如下:echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));
输出

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

示例四

Rule代码
复制代码 代码如下:'http://a94b0a6f6db3649c01a585a80fa7efd7.vt.com/dbfcd3f9832b3d444ccfdb2a9da766bb'=>array('a75dfea808c5bbe57b36ff9aeb68f436/host','urlSuffix'=>'.me'),

Action代码:

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

输出

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') ; 经过处理的$_GET['id']
}
//$id = Yii::app()->request->getPost('id'); 经过处理的$_POST['id']
//$id = Yii::app()->request->getParam('id'); //CHttpRequest更多

2)public function actionUpdate($id)  这种不支持多主键,会检查一下到底GET里面有没有id,没有id就直接不允许访问

'sayhello/<name>' => 'post/hello', name是PostController actionHello($name)的参数
'post/<alias:[-a-z]+>' => 'post/view',  domain/post/e文小写 其中:前面的alias是PostController actionView($alias)的参数
'(posts|archive)/<order:(DESC|ASC)>' => 'post/index', domain/posts/DESC或domain/posts/ASC
'(posts|archive)' => 'post/index', domain/posts或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.

隐藏 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

3.开启rewrite

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

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • YiiFramework入门知识点总结(图文教程)
  • Yii入门教程之目录结构、入口文件及路由设置
  • Yii入门教程之Yii安装及hello world
  • Yii PHP Framework实用入门教程(详细介绍)
  • Yii查询生成器(Query Builder)用法实例教程
  • Yii实现单用户博客系统文章详情页插入评论表单的方法
  • Yii中CGridView实现批量删除的方法
  • Yii快速入门经典教程

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1085880.htmlTechArticleYII使用url组件美化管理的方法,yiiurl组件美化 本文实例讲述了YII使用url组件美化管理的方法。分享给大家供大家参考,具体如下: urlMana...
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