Summary of Url production issues in yii yii framework
Jul 29, 2016 am 08:47 AMCopy the code The code is as follows:
<?php echo CHtml::link('wrong link','user/register')?>
<?php echo CHtml::link('correct Link',array('user/register'))?>
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(
'<controller:w+>/<id:d+>'=> '<controller>/view',
'<controller:w+>/<action:w+>/<id:d+>'=>'<controller>/<action> ;',
'<controller:w+>/<action:w+>'=>'<controller>/<action>',
),
),
the above two lines What kind of link address will the code produce?
http://<site-addr>/user/register //Wrong link
http://<site-addr>/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, <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;
}
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.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use PHP framework Yii to develop a highly available cloud backup system

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications?

Symfony vs Yii2: Which framework is better for developing large-scale web applications?

Data query in Yii framework: access data efficiently

How to convert yii objects into arrays or directly output to json format

Yii2 Programming Guide: How to run Cron service

Form builder in Yii framework: building complex forms
