Solving the problem of Url production in Yii framework

不言
Release: 2023-04-01 13:40:02
Original
1287 people have browsed it

A summary of Url production issues in the yii framework, friends in need can refer to it.

The code is as follows:

<?php echo CHtml::link(&#39;错误链接&#39;,&#39;user/register&#39;)?> 
<?php echo CHtml::link(&#39;正确链接&#39;,array(&#39;user/register&#39;))?>
Copy after login

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

&#39;urlManager&#39;=>array( 
&#39;urlFormat&#39;=>&#39;path&#39;, 
&#39;rules&#39;=>array( 
&#39;<controller:\w+>/<id:\d+>&#39;=>&#39;<controller>/view&#39;, 
&#39;<controller:\w+>/<action:\w+>/<id:\d+>&#39;=>&#39;<controller>/<action>&#39;, 
&#39;<controller:\w+>/<action:\w+>&#39;=>&#39;<controller>/<action>&#39;, 
), 
),
Copy after login

What kind of links will the above two lines of code produce? address?
http:///user/register //Wrong link
http:///index.php/user/register //Correct link
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 UserController's registerAction, which has special meaning.
Attached is the source code of Yii's method for processing 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(&#39;post/list&#39;, &#39;page&#39;=>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=&#39;&#39;; 
} 
return $url===&#39;&#39; ? Yii::app()->getRequest()->getUrl() : $url; 
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the usage of yii paging component

About yii using bootstrap paging style

Analysis of Yii2.0 table association query

The above is the detailed content of Solving the problem of Url production in Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
url
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!