Yii note --- The redirect Method of redirect redirection Yii is defined in both CControler and CHttpRequest. redirect in CController calls The redirect Method in CHttpRequest. We usually call The redirect Method & nbsp in CControoler; Yii notes in framewokwebCController --- redirect redirection
The redirect Method of Yii is defined in both CControler and CHttpRequest. redirect in CController calls The redirect Method in CHttpRequest. We usually call The redirect Method in CControoler.
Definition in framewok/web/CController
1 public function redirect($url,$terminate=true,$statusCode=302)2 {3 if(is_array($url))4 {5 $route=isset($url[0]) ? $url[0] : '';6 $url=$this->createUrl($route,array_splice($url,1));7 }8 Yii::app()->getRequest()->redirect($url,$terminate,$statusCode);9 }
Parameter description:
[Email protected]: specifies the url link to which the browser jumps. if $ url is an array, the first element of the array is composed of controller/method [controller/action, the rest is regarded as the GET parameter, name-value pair, and the createUrl method is called to generate the url. For The redirect Method in framework/web/CHttpRequest. php directly called by a string.
[Email protected]: whether to terminate the current application after calling redirect.
[Email protected]: indicates the HTTP status code. the default value is 302 redirection.
About the array_splice function: remove a part of the array and replace it with other values. the preceding array_splice ($ url, 1) indicates removing the first element of the $ url array, GET the value of the GET parameter
array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement ]] )
About the createUrl function: This function is defined in multiple 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:
1 public function createUrl($route,$params=array(),$ampersand='&') 2 { 3 if($route==='') 4 $route=$this->getId().'/'.$this->getAction()->getId(); 5 elseif(strpos($route,'/')===false) 6 $route=$this->getId().'/'.$route; 7 if($route[0]!=='/' && ($module=$this->getModule())!==null) 8 $route=$module->getId().'/'.$route; 9 return Yii::app()->createUrl(trim($route,'/'),$params,$ampersand);10 }
Here we can see several situations:
1. if redirect does not contain a parameter, $ route is null and will be directed to the current method of the current controller $ route = $ this-> getId (). '/'. $ this-> getAction ()-> getId ();
2. if $ route does not contain '/', for example, $ this-> render ('index', array ('post' => $ questions )); the program will automatically obtain the current controller method ID if only the method is connected and no controller is available.
3. the route contains the '/' character but is not in the first position, and checks whether the current controller is in the module. for example, $ this-> redirect (array ('step/show ', 'id' => 1). In this case, the program automatically determines whether it is a module. We do not need to keep up with the module name when calling createUrl, if you call the method in the main controller in the module, you can add the '/' character to the first letter. In addition, the program will remove the/characters before and after $ route at the end.
Definition in framework/web/CHttpRequest. php
1 public function redirect($url,$terminate=true,$statusCode=302)2 {3 if(strpos($url,'/')===0 && strpos($url,'//')!==0)4 $url=$this->getHostInfo().$url;5 header('Location: '.$url, true, $statusCode);6 if($terminate)7 Yii::app()->end();8 }
If the $ url parameter of redirect in CController is not an array, this function is called directly. if $ url does not start with '/', it will jump directly, in this case, the redirection fails in the module. Therefore, we recommend that you call CController. in php, the redirect method uses arrays as parameters for transmission.
From this we can see that the redirect method still calls the original php header function.
Yii: app ()-> end (); the exit () function of php is called directly.