yii框架中的Url生产问题小结


Posted in PHP onJanuary 16, 2012
<?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; 
}
PHP 相关文章推荐
php empty() 检查一个变量是否为空
Nov 10 PHP
php操作SVN版本服务器类代码
Nov 27 PHP
apache+codeigniter 通过.htcaccess做动态二级域名解析
Jul 01 PHP
深入PHP数据缓存的使用说明
May 10 PHP
有关于PHP中常见数据类型的汇总分享
Jan 06 PHP
PHP中的Memcache详解
Apr 05 PHP
php针对cookie操作的队列操作类实例
Dec 10 PHP
PHP 实现类似js中alert() 提示框
Mar 18 PHP
PHP微信分享开发详解
Jan 14 PHP
深入理解Yii2.0乐观锁与悲观锁的原理与使用
Jul 26 PHP
PHP生成随机密码4种方法及性能对比
Dec 11 PHP
详解Laravel框架的依赖注入功能
May 27 PHP
fgetcvs在linux的问题
Jan 15 #PHP
PHP异步调用socket实现代码
Jan 12 #PHP
114啦源码(114la)不能生成地方房产和地方报刊问题4级页面0字节的解决方法
Jan 12 #PHP
PHP警告Cannot use a scalar value as an array的解决方法
Jan 11 #PHP
Trying to clone an uncloneable object of class Imagic的解决方法
Jan 11 #PHP
win2003服务器使用WPS的COM组件的一些问题解决方法
Jan 11 #PHP
php通过COM类调用组件的实现代码
Jan 11 #PHP
You might like
php的urlencode()URL编码函数浅析
2011/08/09 PHP
PHP中将网页导出为Word文档的代码
2012/05/25 PHP
PHP中::、-&amp;gt;、self、$this几种操作符的区别介绍
2013/04/24 PHP
Server.HTMLEncode让代码在页面里显示为源代码
2013/12/08 PHP
php汉字转拼音的示例
2014/02/27 PHP
SSO单点登录的PHP实现方法(Laravel框架)
2016/03/23 PHP
Thinkphp集成抖音SDK的实现方法
2020/04/28 PHP
javascript编程起步(第五课)
2007/02/27 Javascript
JavaScript中出现乱码的处理心得
2009/12/24 Javascript
JS对select控件option选项的增删改查示例代码
2013/10/21 Javascript
jQuery中ajax的使用与缓存问题的解决方法
2013/12/19 Javascript
js/jquery解析json和数组格式的方法详解
2014/01/09 Javascript
为jquery的ajaxfileupload增加附加参数的方法
2014/03/04 Javascript
Node.js中的process.nextTick使用实例
2015/06/25 Javascript
AngularJS+Node.js实现在线聊天室
2015/08/28 Javascript
基于jQuery Ajax实现下拉框无刷新联动
2017/12/06 jQuery
微信小程序实现分享朋友圈的图片功能示例
2019/01/18 Javascript
jQuery实现为table表格动态添加或删除tr功能示例
2019/02/19 jQuery
Electron实现应用打包、自动升级过程解析
2020/07/07 Javascript
[04:29]DOTA2亚洲邀请赛小组赛第一日 TOP10精彩集锦
2015/02/01 DOTA
[04:59]DOTA2-DPC中国联赛 正赛 Ehome vs iG 选手采访
2021/03/11 DOTA
python中使用OpenCV进行人脸检测的例子
2014/04/18 Python
Python正则表达式教程之二:捕获篇
2017/03/02 Python
pycharm创建一个python包方法图解
2019/04/10 Python
Python pip安装第三方库实现过程解析
2020/07/09 Python
TensorFlow2.0使用keras训练模型的实现
2021/02/20 Python
美国在线眼镜商城:Eyeglasses.com
2017/06/26 全球购物
什么是用户模式(User Mode)与内核模式(Kernel Mode) ?
2014/07/21 面试题
测绘工程个人的自我评价
2013/11/10 职场文书
公司成立感言
2014/01/11 职场文书
公司端午节活动方案
2014/02/04 职场文书
公司活动方案范文
2014/03/06 职场文书
工作经常出错的检讨书
2014/09/13 职场文书
店铺转让协议书
2014/12/02 职场文书
Python自动化爬取天眼查数据的实现
2021/06/15 Python
swagger如何返回map字段注释
2021/07/03 Java/Android