yii url
来源:互联网 发布:尚观怎么样 知乎 编辑:程序博客网 时间:2024/06/05 02:30
<?php echo CHtml::link('错误链接','user/register')?> <?php echo CHtml::link('正确链接',array('user/register'))?>
假定设定了UrlManager的配置为Path模式,用yii默认的配置:
'urlManager'=>array( 'urlFormat'=>'path', 'rules'=>array( '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ),
上面两行代码会生产什么样的链接地址?
http://<site-addr>/user/register //错误链接
http://<site-addr>/index.php/user/register //正确链接
第一个链接是错误的,浏览器会返回404错误。第二个链接会访问UserController的Register方法。区别就在于第二个链接在生成的时候我们传入的参数是一个array数组,而第一个方法是一个简单字符串。Yii在处理Url的时候,遇到简单字符串会直接使用该字符串作为最终的Url,而当遇到数组的时候会调用Controller的CreateUrl来生成Url.
说到简单字符串,这两个链接中其实有一个非常本质的区别。虽然同样都是字符串'user/register',但是在第一个字符串中就代表一个13个字符的相对路径,而第二个链接中则代表UserController的registerAction,是有着特俗意义的。
附上Yii处理Url的方法NormalizeUrl的源代码:
/** * Normalizes the input parameter to be a valid URL. * * If the input parameter is an empty string, the currently requested URL will be returned. * * If the input parameter is a non-empty string, it is treated as a valid URL and will * be returned without any change. * * If the input parameter is an array, it is treated as a controller route and a list of * GET parameters, and the {@link CController::createUrl} method will be invoked to * create a URL. In this case, the first array element refers to the controller route, * and the rest key-value pairs refer to the additional GET parameters for the URL. * For example, <code>array('post/list', 'page'=>3)</code> may be used to generate the URL * <code>/index.php?r=post/list&page=3</code>. * * @param mixed $url the parameter to be used to generate a valid URL * @return string the normalized URL */ public static function normalizeUrl($url) { if(is_array($url)) { if(isset($url[0])) { if(($c=Yii::app()->getController())!==null) $url=$c->createUrl($url[0],array_splice($url,1)); else $url=Yii::app()->createUrl($url[0],array_splice($url,1)); } else $url=''; } return $url==='' ? Yii::app()->getRequest()->getUrl() : $url; }
转 http://www.cnblogs.com/rader/archive/2012/01/15/2323019.html
另外也有一篇文章写的还不错:
http://dongbeta.com/2010/03/yii-create-url/
- yii url
- yii url 重新
- Yii 简化URL
- Yii url重写
- YII url美化管理
- yii 下获取url
- Yii Url重写
- yii url优化
- Yii url规则重写
- yii 常用url地址
- 【Yii】url路由
- Yii的常用URL
- Yii的URL管理
- Yii的常用URL
- Yii的常用URL
- Yii的常用URL
- YII url美化管理
- Yii url 工具类
- 怎么写makefile?
- 什么是watchdog + 为何在要系统初始化的时候关闭watchdog
- 3-14面试
- 一个优秀的VC++任务栏托盘源码
- 第二次C程序设计作业
- yii url
- 实战DeviceIoControl 之一:通过API访问设备驱动程序
- android apk 反编译与防止反编译
- 实战DeviceIoControl 之二:获取软盘/硬盘/光盘的参数
- Yii url重写
- ndk 编译常见错误-你懂得
- 搞定ios静态库
- Google源码技巧(一)
- 实战DeviceIoControl 之三:制作磁盘镜像文件