Yii’s redirect method is defined in CControler and CHttpRequest, and the redirect in CController is called The redirect method in CHttpRequest. What we usually call is the redirect method in CControoler
Definition in framewok/web/CController
<span>1</span> <span>public</span> <span>function</span> redirect(<span>$url</span>,<span>$terminate</span>=<span>true</span>,<span>$statusCode</span>=302<span>) </span><span>2</span> <span>{ </span><span>3</span> <span>if</span>(<span>is_array</span>(<span>$url</span><span>)) </span><span>4</span> <span> { </span><span>5</span> <span>$route</span>=<span>isset</span>(<span>$url</span>[0]) ? <span>$url</span>[0] : ''<span>; </span><span>6</span> <span>$url</span>=<span>$this</span>->createUrl(<span>$route</span>,<span>array_splice</span>(<span>$url</span>,1<span>)); </span><span>7</span> <span> } </span><span>8</span> Yii::app()->getRequest()->redirect(<span>$url</span>,<span>$terminate</span>,<span>$statusCode</span><span>); </span><span>9</span> }
Parameter description:
@url: Specifies the url link that the browser jumps to. If $url is an array, the first element of the array is composed of the controller/method [controller/action], and the remaining parts are regarded as GET parameters. , the name-value pair and the createUrl method is called to generate the url. If it is a string, directly call the redirect method in framework/web/CHttpRequest.php.
@terminate: Whether to terminate the current application after calling redirect.
@statusCode: Represents the HTTP status code, the default is 302 redirect
About the array_splice function: Remove part of the array and replace it with other values. The above array_splice($url,1) means to remove the first element of the $url array and obtain the value of the GET parameter.
<span>array</span> <span>array_splice</span> ( <span>array</span> &<span>$input</span> , int <span>$offset</span> [, int <span>$length</span> = 0 [, <span>mixed</span> <span>$replacement</span> ]] )
About the createUrl function: This function is defined in many places like redirect, in CController.php and CurlManager.php respectively. The final definition is in CurlManager.php.
The following is the definition of createURL in CController:
<span> 1</span> <span>public</span> <span>function</span> createUrl(<span>$route</span>,<span>$params</span>=<span>array</span>(),<span>$ampersand</span>='&'<span>) </span><span> 2</span> <span> { </span><span> 3</span> <span>if</span>(<span>$route</span>===''<span>) </span><span> 4</span> <span>$route</span>=<span>$this</span>->getId().'/'.<span>$this</span>->getAction()-><span>getId(); </span><span> 5</span> <span>elseif</span>(<span>strpos</span>(<span>$route</span>,'/')===<span>false</span><span>) </span><span> 6</span> <span>$route</span>=<span>$this</span>->getId().'/'.<span>$route</span><span>; </span><span> 7</span> <span>if</span>(<span>$route</span>[0]!=='/' && (<span>$module</span>=<span>$this</span>->getModule())!==<span>null</span><span>) </span><span> 8</span> <span>$route</span>=<span>$module</span>->getId().'/'.<span>$route</span><span>; </span><span> 9</span> <span>return</span> Yii::app()->createUrl(<span>trim</span>(<span>$route</span>,'/'),<span>$params</span>,<span>$ampersand</span><span>); </span><span>10</span> }
We can see several situations from here:
1. If redirect does not take parameters and $route is empty, it will be directed to the current method of the current controller $route=$this->getId().'/'.$this->getAction ()->getId();
2. If there is no '/' in $route, for example $this->render('index',array('post'=>$questions)); only connects the method without the controller, the program The current controller method ID will be automatically obtained
3. There is a '/' character in the route, but it is not at the first position, and check whether the current controller is in the module; for example, $this->redirect(array('step/show','id'=> ;1)); In this case, the program will automatically determine whether it is a module. We don’t need to follow the name of the module when calling createUrl. If we call the method in the main controller in the module, we can Add the '/' character to the letter. And the program will remove the / characters before and after $route at the end.
Definition in framework/web/CHttpRequest.php
<span>1</span> <span>public</span> <span>function</span> redirect(<span>$url</span>,<span>$terminate</span>=<span>true</span>,<span>$statusCode</span>=302<span>) </span><span>2</span> <span> { </span><span>3</span> <span>if</span>(<span>strpos</span>(<span>$url</span>,'/')===0 && <span>strpos</span>(<span>$url</span>,'//')!==0<span>) </span><span>4</span> <span>$url</span>=<span>$this</span>->getHostInfo().<span>$url</span><span>; </span><span>5</span> <span>header</span>('Location: '.<span>$url</span>, <span>true</span>, <span>$statusCode</span><span>); </span><span>6</span> <span>if</span>(<span>$terminate</span><span>) </span><span>7</span> Yii::app()-><span>end</span><span>(); </span><span>8</span> }
If the $url parameter of redirect in CController is not an array, this function will be called directly. If $url does not start with '/', it will jump directly. This situation will cause the redirection in the module to fail. So it is recommended to use array as a parameter when calling the redirect method in CController.php
From this we can see that the redirect method ultimately calls the PHP native header function
Yii::app()->end(); directly calls the exit() function of PHP.