Yii Notes---Redirect redirect, Yii---redirect
The redirect method of Yii is defined in both Ccontroler and CHttpRequest, and redirect in Ccontroller is called CHttpRequest method in redirect. What we normally call is the redirect method in Ccontrooler.
Definitions in Framewok/web/ccontroller
1 Public function Redirect ($url,$terminate=true,$statusCode=302) 2 {3 if (is_array($url)) 4 {5 $route=isset($url$url[0]: " ; 6 $url=$this->createurl ($route,array_splice($url, 1 )); 7 }8 Yii::app ()->getrequest ()->redirect ($url,$terminate ,$statusCode); 9 }
Parameter description:
@url: Specifies the URL link to which the browser jumps, and if the controller/action is an array, the first element of the arrays is made up of the controller/method "X", and the remainder is treated as a get parameter. Name-value to and calls the CreateURL method to generate the URL. if it is a string, call the redirect method in the framework/web/chttprequest.php directly.
@terminate: Whether to terminate the current app after calling redirect.
@statusCode: Indicates the status code of HTTP, default is 302 redirect
About the Array_splice function: Remove part of the array and replace it with other values, above the Array_splice ($url, 1) to remove the first element of the hash array, get the value of the Get parameter
Array Array_splice array &$input $offset $length = 0 mixed $replacement ]] )
About the CreateURL function: This function is defined in many places as the redirect, respectively, in ccontroller.php and curlmanager.php. The final definition is among the curlmanager.php.
The following is the definition of CreateURL in Ccontroller:
1 Public functionCreateURL ($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 returnYii::app ()->createurl (Trim($route,'/'),$params,$ampersand);Ten}
Here are a few things to see:
1. If redirect has no parameters then $route is empty, it will be directed to the current method of the current controller $route = $this->getid (). ' /'. $this->getaction ()->getid ();
2, if the $route without '/', for example $this->render (' index ', array (' post ' = + $questions)), and only the method and no controller, the program will automatically get to the current controller method ID
3, the route has the '/' character, but not the first position, and find whether the current controller is in the module, for example $this->redirect (' step/show ', ' id ' =>1); In this case, the program will automatically determine whether it is a module, we can call the CreateURL when we do not have to keep up with the name of the module, if we call the method in the main controller in the module we can add the '/' character at the first letter. and the program at the end will be removed before and after $route/characters.
Definitions in framework/web/chttprequest.php
1 Public function Redirect ($url,$terminate=true,$statusCode=302) 2 {3 if(strpos($urlstrpos($url , '///')!==0)4 $url=$this->gethostinfo (). $url; 5 Header (' Location: '. $url true $statusCode ); 6 if ($terminate) 7 Yii::app (),end(); 8 }
If the redirect in Ccontroller is not an array, the function will be called directly if it is not preceded by '/', which causes redirection to fail in the module. Therefore, it is recommended that the redirect method be passed as an array parameter when calling ccontroller.php .
It can be seen from this that the redirect method is ultimately called the PHP primitive's header function
Yii::app ()->end (); The exit () function of PHP is called directly.
http://www.bkjia.com/PHPjc/976449.html www.bkjia.com true http://www.bkjia.com/PHPjc/976449.html techarticle Yii Notes---Redirect redirection, Yii---The redirect method of redirect Yii is defined in Ccontroler and CHttpRequest, The redirect in Ccontroller called the redirect party in the CHttpRequest ...