yii的官方文档对此的解释如下:

urlSuffix  此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null。例如可以将此设置为.html,让url看起来“像”是一个静态页面。

caseSensitive  是否大小写敏感,默认使用CUrlManager::caseSensitive,值为null。

defaultParams  该规则使用的默认get参数。当使用该规则来解析一个请求时,这个参数的值会被注入到$_GET参数中。

matchValue  当创建一个URL时,GET参数是否匹配相应的子模式。默认使用CurlManager::matchValue,值为null。

如果该属性为 false,那么意味着当路由和参数名匹配给定的规则时,将以此来创建一个URL。

如果该属性为true,那么给定的参数值夜必须匹配相应的参数子模式。

注意:将此属性设置为true会降低性能。

我们使用一些例子来解释网址工作规则。我们假设我们的规则包括如下三个:

  1. array(
  2. 'posts'=>'post/list',
  3. 'post/<id:\d+>'=>'post/read',
  4. 'post/<year:\d{4}>/<title>'=>'post/read',
  5. )

调用$this->createUrl('post/list')生成/index.php/posts。第一个规则适用。

调用$this->createUrl('post/read',array('id'=>100))生成/index.php/post/100。第二个规则适用。

调用$this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post'))生成/index.php/post/2008/a%20sample%20post。第三个规则适用。

调用$this->createUrl('post/read')产生/index.php/post/read。请注意,没有规则适用。

总之,当使用createUrl生成网址,路线和传递给该方法的GET参数被用来决定哪些网址规则适用。如果关联规则中的每个参数可以在GET参数找到的,将被传递给createUrl ,如果路线的规则也匹配路线参数,规则将用来生成网址。

如果GET参数传递到createUrl是以上所要求的一项规则,其他参数将出现在查询字符串。例如,如果我们调用$this->createUrl('post/read',array('id'=>100,'year'=>2008)) ,我们将获得/index.php/post/100?year=2008。为了使这些额外参数出现在路径信息的一部分,我们应该给规则附加/* 。 因此,该规则post/<id:\d+>/* ,我们可以获取网址/index.php/post/100/year/2008 。

正如我们提到的,URL规则的其他用途是解析请求网址。当然,这是URL生成的一个逆过程。例如, 当用户请求/index.php/post/100 ,上面例子的第二个规则将适用来解析路线post/read和GET参数array('id'=>100) (可通过$_GET获得) 。

提示:此网址通过createurl方法所产生的是一个相对地址。为了得到一个绝对的url ,我们可以用前缀yii: :app()->hostInfo ,或调用createAbsoluteUrl 。
注:使用的URL规则将降低应用的性能。这是因为当解析请求的URL ,[ CUrlManager ]尝试使用每个规则来匹配它,直到某个规则可以适用。因此,高流量网站应用应尽量减少其使用的URL规则。
 
test.com/vthot 想生成 test.com/vthot/

  1. 'urlSuffix'=>'/',

要更改URL格式,我们应该配置urlManager应用元件,以便createUrl可以自动切换到新格式和应用程序可以正确理解新的网址:

  1. 'urlManager'=>array(
  2. 'urlFormat'=>'path',
  3. 'showScriptName'=>false,
  4. 'urlSuffix'=>'.html',
  5. 'rules'=>array(
  6. 'posts'=>'post/list',
  7. 'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),
  8. 'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),
  9. ),
  10. ),

示例一

Rule代码

  1. 'posts'=>'post/list',

Action代码

  1. echo $this->createAbsoluteUrl('post/list');

输出

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

示例二

Rule代码

  1. 'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),

Action代码

  1. echo $this->createAbsoluteUrl('post/show',array('id'=>998, 'name'=>'123'));

输出

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

示例三

Rule代码

  1. 'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),

Action代码

  1. echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));

输出

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

示例四

Rule代码

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

Action代码

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

输出

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

1)controller/Update/id/23

  1. public function actionUpdate(){
  2. $id = Yii::app()->request->getQuery('id') ; 经过处理的$_GET['id']
  3. }
  4. //$id = Yii::app()->request->getPost('id'); 经过处理的$_POST['id']
  5. //$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

  1. RewriteEngine on
  2. # if a directory or a file exists, use it directly
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. # otherwise forward it to index.php
  6. RewriteRule . index.php

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

----------------------------

补充一点:如果域名为www.test.com 希望www.test.com/112 url 修改为www.test.com/indexphp?r=index/action?key=112

在url 里记得修改key,url里的取值是用<name:value> 来取的,记得看下面标注红色部分

'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false, //隐藏index.php
'urlSuffix' => '.html', //后缀
'caseSensitive' => true, //设置对大小写不敏感
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<get:\w+>'=>'api/index/get/<get>/',
),
),

最新文章

  1. 0025 Java学习笔记-面向对象-final修饰符、不可变类
  2. 动态sql语句输出参数
  3. 宏定义中的##操作符和... and _ _VA_ARGS_ _
  4. grunt构建前端自动化的开发环境
  5. Full GC有关问题学习分析(转载)
  6. grunt + compass retina sprites
  7. 最小化安装CentOS7 + xfce4 +PHP + nginx +mariadb 开发环境
  8. nyoj 16 矩形嵌套
  9. csdn博客又開始更新了
  10. 1.2 Coin 项目
  11. Yii的场景
  12. 关于python如果没有numpy模块如何处理
  13. dp百题大过关(第一场)
  14. JFinal实现伪静态
  15. Python:logging.NullHandler 的使用
  16. thinkphp 视图(二)变量输出、赋值和替换
  17. NOIP2018ty记
  18. windows 下 wamp php单元测试工具PHPUnit的安装
  19. java 动手动脑解决问题
  20. C# TextWriter类

热门文章

  1. Python Django 开发 3 数据库CURD
  2. Windows 10 Weather App无法正常显示解决方法
  3. Touch Event
  4. pptv破解版程序,能够免费观看所有蓝光和会员影片!
  5. SQLServer的数据类型
  6. MyBatis知多少(7)持久层
  7. Rpath handling on Linux
  8. JS移动端滑屏事件
  9. JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)
  10. MPU9250调试