Home > php教程 > php手册 > Yii笔记---redirect重定向,yii---redirect

Yii笔记---redirect重定向,yii---redirect

WBOY
Release: 2016-06-13 09:08:36
Original
1057 people have browsed it

Yii笔记---redirect重定向,yii---redirect

Yii的redirect方法在CControler与CHttpRequest之中都有被定义,CController中的redirect调用了CHttpRequest中的redirect方法。我们平常调用的是CControoler中的redirect方法

 

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> }
Copy after login

参数说明:

  @url:指定浏览器跳转到的url链接,如果$url为数组,则数组的第一个元素是由控制器/方法【controller/action】组成,其余的部分被视为GET参数,name-value对并调用了createUrl方法生成url。如果是字符串 直接调用的framework/web/CHttpRequest.php中的redirect方法。

  @terminate:调用redirect之后是否终止当前的应用。

  @statusCode:表示HTTP的状态码,默认是302重定向

关于array_splice函数:把数组中的一部分去掉并用其它值取代,上面的array_splice($url,1)表示的是将$url数组的第一个元素去掉,获取到GET参数的值

<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>  ]] )
Copy after login

关于createUrl函数:这个函数和redirect一样在多处有定义,分别在CController.php和CurlManager.php之中。最终的定义在CurlManager.php之中。

下面是CController中的createURL的定义:

<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>     }
Copy after login

从这里可以看出来几种情况:

  1、如果redirect没有带参数则$route为空的情况,会被定向到 当前控制器的当前方法 $route=$this->getId().'/'.$this->getAction()->getId();

  2、如果$route中不带‘/’,例如 $this->render('index',array('post'=>$questions));只接了方法而没有控制器,程序会自动获取到当前的控制器方法ID

  3、route中有‘/’字符,但是不在首位置,并且查找当前控制器是否位于模块之中;例如 $this->redirect(array('step/show','id'=>1)); 这种情况程序会自动判断是否是模块,我们在调用createUrl的时候就可以不用跟上模块的名称,如果在模块中调用主控制器中的方法时 我们可以在首字母处加上'/'字符。并且程序在最后都会去掉$route前后的/字符。

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>     }
Copy after login

 

如果CController之中的redirect的$url参数不是数组,则会直接调用该函数,如果$url不是以'/'开头则会直接跳转,这种情况导致在模块中重定向失败,所以建议在调用CController.php之中redirect方法时都是用数组作为参数进行传递

从这可以看出redirect方法最终还是调用的php原生态的header函数

Yii::app()->end(); 直接调用的是php的exit()函数。

 

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template