yii框架中的Url生产问题小结
程序员文章站
2022-05-25 21:09:51
复制代码 代码如下:
<?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 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;
}