Yii Notes-redirect redirect

Source: Internet
Author: User
Tags yii
Yii Notes---redirect redirection

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 functionredirect$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     }8Yii::app ()->getrequest ()->redirect ($url,$terminate,$statusCode);9}

Parameter description:

[Email protected]: Specifies the URL link to which the browser jumps, and if the number is an array, the first element of the arrays is composed of the controller/method "Controller/action", 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.

[Email protected]: whether to terminate the current app after calling redirect.

[Email protected]: 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 functionredirect$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)7Yii::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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.