国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

yii框架中的Url生產問題小結

2024-05-04 23:15:49
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


<?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>',
),
),


上面兩行代碼會生產什么樣的鏈接地址?
<site-addr>/user/register //錯誤鏈接
<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;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 民权县| 武邑县| 鄯善县| 美姑县| 阿克| 比如县| 额敏县| 璧山县| 长岛县| 江油市| 四子王旗| 广南县| 五家渠市| 叶城县| 桦川县| 虎林市| 思茅市| 荆门市| 浦江县| 沛县| 藁城市| 平利县| 阳西县| 岳阳县| 鄂托克前旗| 永年县| 梅州市| 阿克苏市| 本溪市| 太谷县| 朝阳市| 桃源县| 会昌县| 宜春市| 九寨沟县| 澎湖县| 呼玛县| 濉溪县| 遂昌县| 遂平县| 阳山县|