Home  >  Article  >  Backend Development  >  Summary of Url production issues in yii framework_PHP tutorial

Summary of Url production issues in yii framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:21:15815browse

Copy code The code is as follows:




Assume that the configuration of UrlManager is set to Path mode, use Yii default configuration:
Copy code The code is as follows:

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

What kind of link address will the above two lines of code produce?
http:///user/register //Wrong link
http:///index.php/user/register //Correct link
No. If a 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 There is actually a very essential difference between the two links. Although they are also the string 'user/register', the first string represents a 13-character relative path, while the second link represents the 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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324933.htmlTechArticleCopy the code as follows: ?php echo CHtml::link('wrong link','user/register') ??php echo CHtml::link('Correct link',array('user/register'))? Assume that the configuration of UrlManager is set to...
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