Yii之url路由管理

url路由规则配置在protected/config/main.php中urlManager = array();其继承的类CUrlManager类

URL路由,是指通过分析URL,找出请求的控制器及动作。路由信息就是指控制器及动作组成的请求字符串,形如controllerID/actionID

1.Yii中的路由支持两种格式

  1.path’格式:localhost/y_demo/jiu/index.php/test/get/id/3

  2.get’格式:localhost/y_demo/jiu/index.php?r=test/get&id=3

2.Yii中的路由主要有两重作用:

  1.根据请求URL找到应对的控制器及方法

  2.根据提供的参数及规则生成URL

3.URL路由处理由urlManager组件,其可以设置的属性

  1.$rules = array(); //#url双向解析规则,这是最复姓的一项参数

  2.$urlSuffix=’.html’; //#pathinfo模式时的url后缀

  3.$showScriptName=true; //是否在URL中显示脚本文件名称,当设置为false时需要服务器支持

  4.$appendParams=true; //#pathinfo模式下是否将参数追加到URL上,此项参数一般用于生成URL场景。设置为true时,参数将以斜线分隔并追加到路径后面。设为false时将以query string方式追加

  5.$routeVar=’r’; //#仅在get形式时有效,指定路由信息的变量名称

  6.$caseSensitive=true; //#路由信息是否区分大小写,如果设置为false,请求路由信息被转换成小写,所有CWebApplication::controllerMap/CController::actions、保存控制器的目录三者都应该使用小写

  7.$cacehID=’cache’; //#路由缓存所使用的组件名称(若使用缓存机制)

  8.$useStrictParsing=false; //#仅在Path形式时有效,设置是否使用严格的URL解析。如果设置为true.则任何$urlRuleClass=’CUrlRule’

4.urlManager的配置

'urlManager'=>array(
            'urlFormat'=>'path',    //使用pathinfo路径模式
            'showScriptName'=>false,    //去掉index.php
            'urlSuffix'=>'.html',   //添加.html的后缀
            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                '<_m:\w+>/<_c:\w+>/<_a:\w+>'=>'<_m>/<_c>/<_a>',
            ),
        ),

5.实例

路由:
$this->render('post/view',array( 'id'=>23, 'title'=>'text_title', 'name1'=>'value1', 'name2'=>'value2', )); //其原地址 index.php?r=post/view&id=23&title=text_title&name1=value1&name2=value2 //设置路由规则 'urlManager'=>array( 'urlFormat'=>'path', 'rules'=>array( 'post/<id:\d+>/<title:.*?>'=>'post/view', ), ), //使用规则后的地址 index.php/post/23/text_title?name1=value1&name2=value2

 

posted @ 2014-10-19 23:12  答案在我心中  阅读(1870)  评论(0编辑  收藏  举报