yii框架中的Url生产问题小结_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了yii框架中的Url生产问题小结_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2322字,纯文字阅读大概需要4分钟。
内容图文
![yii框架中的Url生产问题小结_PHP教程](/upload/InfoBanner/zyjiaocheng/170/36c1940cff064b918dcf51afa9c27443.jpg)
假定设定了UrlManager的配置为Path模式,用yii默认的配置:
代码如下:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'/view',
'<controller:\w+>//<id:\d+>'=>'/',
'<controller:\w+>/'=>'/',
),
),
上面两行代码会生产什么样的链接地址?
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,array('post/list', 'page'=>3)
may be used to generate the URL
*/index.php?r=post/list&page=3
.
*
* @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.bkjia.com/PHPjc/324933.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324933.htmlTechArticle 代码如下:
?php echo CHtml::link('错误链接','user/register')? ?php echo CHtml::link('正确链接',array('user/register'))? 假定设定了UrlManager的配置为...
内容总结
以上是互联网集市为您收集整理的yii框架中的Url生产问题小结_PHP教程全部内容,希望文章能够帮你解决yii框架中的Url生产问题小结_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。