Summary of Url production issues in the yii Framework. Copy the code as follows :? PhpechoCHtml: link (error link, userregister )?? PhpechoCHtml: link (correct link, array (userregister ))? Assume that the UrlManager configuration is set
The code is as follows:
Assume that the URL manager configuration is set to the Path mode. use the default yii configuration:
The code is as follows:
'Urlmanager' => array (
'Urlformat' => 'path ',
'Rules' => array (
' / '=>' /View ',
' // '=>' /',
' /'=>' /',
),
),
What link addresses will the above two lines of code produce?
Http :// /User/register // error link
Http :// /Index. php/user/register // correct link
The first link is incorrect. the browser returns error 404. The second link will access the Register method of UserController. The difference is that when the second link is generated, the input parameter is an array, and the first method is a simple string. When processing a Url, Yii directly uses this string as the final Url when encountering a simple string, and calls the CreateUrl of Controller to generate a Url when encountering an array.
Speaking of simple strings, there is actually a very essential difference between the two links. Although both strings are 'user/register ', the first string represents a 13-character relative path, and the second link represents the registerAction of UserController, is of special significance.
Attaches the source code of NormalizeUrl of Yii's Url processing method:
The code is as follows:
/**
* 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
* GET parameters, and the {@ link CController: createUrl} method will be invoked
* 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;
}
The http://www.bkjia.com/PHPjc/324933.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324933.htmlTechArticle code is as follows :? Php echo CHtml: link ('error link', 'user/register ')? ? Php echo CHtml: link ('correct link', array ('user/register '))? Assume that the UrlManager configuration is set...