yii框架中的Url生產(chǎn)問(wèn)題小結(jié)
<?php echo CHtml::link('錯(cuò)誤鏈接','user/register')?>
<?php echo CHtml::link('正確鏈接',array('user/register'))?>
假定設(shè)定了UrlManager的配置為Path模式,用yii默認(rèn)的配置:
'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>',
),
),
上面兩行代碼會(huì)生產(chǎn)什么樣的鏈接地址?
http://<site-addr>/user/register //錯(cuò)誤鏈接
http://<site-addr>/index.php/user/register //正確鏈接
第一個(gè)鏈接是錯(cuò)誤的,瀏覽器會(huì)返回404錯(cuò)誤。第二個(gè)鏈接會(huì)訪問(wèn)UserController的Register方法。區(qū)別就在于第二個(gè)鏈接在生成的時(shí)候我們傳入的參數(shù)是一個(gè)array數(shù)組,而第一個(gè)方法是一個(gè)簡(jiǎn)單字符串。Yii在處理Url的時(shí)候,遇到簡(jiǎn)單字符串會(huì)直接使用該字符串作為最終的Url,而當(dāng)遇到數(shù)組的時(shí)候會(huì)調(diào)用Controller的CreateUrl來(lái)生成Url.
說(shuō)到簡(jiǎn)單字符串,這兩個(gè)鏈接中其實(shí)有一個(gè)非常本質(zhì)的區(qū)別。雖然同樣都是字符串'user/register',但是在第一個(gè)字符串中就代表一個(gè)13個(gè)字符的相對(duì)路徑,而第二個(gè)鏈接中則代表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;
}
相關(guān)文章
PHP使用三種方法實(shí)現(xiàn)數(shù)據(jù)采集
這篇文章主要介紹了PHP使用三種方法實(shí)現(xiàn)數(shù)據(jù)采集,對(duì)數(shù)據(jù)采集感興趣的同學(xué),可以參考下2021-04-04php使用CutyCapt實(shí)現(xiàn)網(wǎng)頁(yè)截圖保存的方法
這篇文章主要介紹了php使用CutyCapt實(shí)現(xiàn)網(wǎng)頁(yè)截圖保存的方法,結(jié)合實(shí)例形式詳細(xì)分析了CutyCapt的下載、安裝及php使用CutyCapt進(jìn)行截圖與保存的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10php 如何設(shè)置一個(gè)嚴(yán)格控制過(guò)期時(shí)間的session
本篇文章主要介紹了php設(shè)置一個(gè)嚴(yán)格控制過(guò)期時(shí)間的session的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05php5與php7的區(qū)別點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于php5與php7的區(qū)別是什么的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們學(xué)習(xí)下。2019-10-10shopex主機(jī)報(bào)錯(cuò)誤請(qǐng)求解決方案(No such file or directory)
最近vps客戶和服務(wù)托管客戶安裝了shopex網(wǎng)店系統(tǒng),他們的主機(jī)都報(bào)錯(cuò),錯(cuò)誤特點(diǎn)一樣,針對(duì)這個(gè)錯(cuò)誤我司技術(shù)推出shopex主機(jī)報(bào)錯(cuò)誤請(qǐng)求解決方案,希望能給予各位一點(diǎn)幫助2011-12-12php結(jié)合mysql與mysqli擴(kuò)展處理事務(wù)的方法
這篇文章主要介紹了php結(jié)合mysql與mysqli擴(kuò)展處理事務(wù)的方法,結(jié)合實(shí)例形式分析了php使用mysql與mysqli處理事務(wù)的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-06-06PHP實(shí)現(xiàn)從遠(yuǎn)程下載文件的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)從遠(yuǎn)程下載文件的方法,涉及php操作文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03php截取字符串并保留完整xml標(biāo)簽的函數(shù)代碼
截取字符串并保留完整xml標(biāo)簽的php代碼,有需要的朋友可以參考下2013-02-02