Home >Backend Development >PHP Tutorial >Summary of Url production issues in yii yii framework

Summary of Url production issues in yii yii framework

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 08:47:42934browse

Copy the code The code is as follows:





Assume that the UrlManager configuration is set to Path mode, and use the yii default configuration:

Copy the code The code is as follows:


' urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'/'=> '/view',
'//'=>'/'/'=>'/',
),
),


the above two lines What kind of link address will the code produce?
http:///user/register //Wrong link
http:///index.php/user/register //Correct link
The first link is wrong , the browser will return a 404 error. The second link will access the Register method of UserController. The difference is that when the second link is generated, the parameter we pass in is an array, while the first method is a simple string. When Yii processes Url, when it encounters a simple string, it will directly use the string as the final Url. When it encounters an array, it will call the Controller's CreateUrl to generate the Url.
Speaking of simple strings, these two links In fact, there is a very essential difference. Although they are also the string 'user/register', the first string represents a 13-character relative path, while the second link represents UserController's registerAction, which has special meaning.
Attached is the source code of Yii's Url processing method NormalizeUrl:

Copy the code 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 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;
}

The above has introduced a summary of Url production issues in the yii yii framework, including yii content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn