YII的forward\redirect

以下代码都是在SiteController中

/**
* This is the default 'index' action that is invoked
* when an action is not explicitly requested by users.
*/
public function actionIndex()

进行的。


1.YII的redirect


 

参数

[php]  view plain copy
  1. ($url,$terminate=true,$statusCode=302)  

$url----url地址 

$terminate是否终止后续程序的执行

$statusCode状态码




使用方法

$this->redirect(array('index'));

对应的是当前controller的index action

http://www.localyii.com/testwebap/index.php?r=user/index

$this->redirect(array('view','id'=>$model->id));

对应的是当前controller的view action并传递id参数值为3

http://www.localyii.com/testwebap/index.php?r=user/view&id=3


$this->redirect(array('/site/contact','id'=>12));

$this->redirect(array('site/contact','id'=>12));


http://www.localyii.com/testwebap/index.php?r=site/contact&id=12


$this->redirect(array('site/contact','id'=>'idv','name'=>'namev'));

http://www.localyii.com/testwebap/index.php?r=site/contact&id=idv&name=namev

$this->redirect(array('site/contact','v1','v2','v3'));

http://www.localyii.com/testwebap/index.php?r=site/contact&0=v1&1=v2&2=v3


$this->redirect(array('site/contact','v1','v2','v3','#'=>'ttt'));

带anchor的

http://www.localyii.com/testwebap/index.php?r=site/contact&0=v1&1=v2&2=v3#ttt


$this->redirect(array('user/create','v1','v2','v3','#'=>'ttt'));

http://www.localyii.com/testwebap/index.php?r=user/create&0=v1&1=v2&2=v3#ttt


modules的redirect

$this->redirect(array('testmod/default/index','v1','v2','v3','#'=>'ttt'));

http://www.localyii.com/testwebap/index.php?r=testmod/default/index&0=v1&1=v2&2=v3#ttt


跳转到一个绝对路径

$this->redirect('http://www.baidu.com');



实现思路

class CController extends CBaseController

[php]  view plain copy
  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.     }  


如果url是数组时,数组的第一个值会当作route。其他的当作参数。

array array_splice ( array &$input , int $offset [, int $length [, array $ replacement ]] )

array_splice — 把数组中的一部分去掉并用其它值取代



<?php
$input 
= array("red""green""blue""yellow");
array_splice($input2);
// $input is now array("red", "green")

$input = array("red""green""blue""yellow");
array_splice($input1, -1);
// $input is now array("red", "yellow")

$input = array("red""green""blue""yellow");
array_splice($input1count($input), "orange");
// $input is now array("red", "orange")

$input = array("red""green""blue""yellow");
array_splice($input, -11, array("black""maroon"));
// $input is now array("red", "green",
//          "blue", "black", "maroon")

$input = array("red""green""blue""yellow");
array_splice($input30"purple");
// $input is now array("red", "green",
//          "blue", "purple", "yellow");
?>

class CHttpRequest extends CApplicationComponent

 

[php]  view plain copy
  1. public function redirect($url,$terminate=true,$statusCode=302)  
  2.     {  
  3.         if(strpos($url,'/')===0)  
  4.             $url=$this->getHostInfo().$url;  
  5.         <strong>header('Location: '.$url, true, $statusCode);</strong>  
  6.         if($terminate)  
  7.             Yii::app()->end();  
  8.     }  

[php]  view plain copy
  1. public function createUrl($route,$params=array(),$ampersand='&')  
  2. {  
  3.     unset($params[$this->routeVar]);  
  4.     foreach($params as &$param)  
  5.         if($param===null)  
  6.             $param='';  
  7.     if(isset($params['#']))  
  8.     {  
  9.         $anchor='#'.$params['#'];  
  10.         unset($params['#']);  
  11.     }  
  12.     else  
  13.         $anchor='';  
  14.     $route=trim($route,'/');  
  15.     foreach($this->_rules as $i=>$rule)  
  16.     {  
  17.         if(is_array($rule))  
  18.             $this->_rules[$i]=$rule=Yii::createComponent($rule);  
  19.         if(($url=$rule->createUrl($this,$route,$params,$ampersand))!==false)  
  20.         {  
  21.             if($rule->hasHostInfo)  
  22.                 return $url==='' ? '/'.$anchor : $url.$anchor;  
  23.             else  
  24.                 return $this->getBaseUrl().'/'.$url.$anchor;  
  25.         }  
  26.     }  
  27.     return $this->createUrlDefault($route,$params,$ampersand).$anchor;  
  28. }  
  29. protected function createUrlDefault($route,$params,$ampersand)  
  30. {  
  31.     if($this->getUrlFormat()===self::PATH_FORMAT)  
  32.     {  
  33.         $url=rtrim($this->getBaseUrl().'/'.$route,'/');  
  34.         if($this->appendParams)  
  35.         {  
  36.             $url=rtrim($url.'/'.$this->createPathInfo($params,'/','/'),'/');  
  37.             return $route==='' ? $url : $url.$this->urlSuffix;  
  38.         }  
  39.         else  
  40.         {  
  41.             if($route!=='')  
  42.                 $url.=$this->urlSuffix;  
  43.             $query=$this->createPathInfo($params,'=',$ampersand);  
  44.             return $query==='' ? $url : $url.'?'.$query;  
  45.         }  
  46.     }  
  47.     else  
  48.     {  
  49.         $url=$this->getBaseUrl();  
  50.         if(!$this->showScriptName)  
  51.             $url.='/';  
  52.         if($route!=='')  
  53.         {  
  54.             $url.='?'.$this->routeVar.'='.$route;  
  55.             if(($query=$this->createPathInfo($params,'=',$ampersand))!=='')  
  56.                 $url.=$ampersand.$query;  
  57.         }  
  58.         else if(($query=$this->createPathInfo($params,'=',$ampersand))!=='')  
  59.             $url.='?'.$query;  
  60.         return $url;  
  61.     }  
  62. }  



2.YII的forward

使用方法

参数

$route

是string。一个url路径

$exit

 $this->forward('/testmod/default/index');

$this->forward('testmod/default/index'); 

地址栏url 

 http://www.localyii.com/testwebap/index.php

$this->forward('testmod/default/index'); 

实现思路

class CController extends CBaseController

 

[php]  view plain copy
  1. /** 
  2.      * Processes the request using another controller action. 
  3.      * This is like {@link redirect}, but the user browser's URL remains unchanged. 
  4.      * In most cases, you should call {@link redirect} instead of this method. 
  5.      * @param string $route the route of the new controller action. This can be an action ID, or a complete route 
  6.      * with module ID (optional in the current module), controller ID and action ID. If the former, the action is assumed 
  7.      * to be located within the current controller. 
  8.      * @param boolean $exit whether to end the application after this call. Defaults to true. 
  9.      * @since 1.1.0 
  10.      */  

[php]  view plain copy
  1. public function forward($route,$exit=true)  
  2.     {  
  3.         if(strpos($route,'/')===false)  
  4.             $this->run($route);  
  5.         else  
  6.         {  
  7.             if($route[0]!=='/' && ($module=$this->getModule())!==null)  
  8.                 $route=$module->getId().'/'.$route;  
  9.             Yii::app()->runController($route);  
  10.         }  
  11.         if($exit)  
  12.             Yii::app()->end();  
  13.     }  

[php]  view plain copy
  1. public function runController($route)  
  2.     {  
  3.         if(($ca=$this->createController($route))!==null)  
  4.         {  
  5.             list($controller,$actionID)=$ca;  
  6.             $oldController=$this->_controller;  
  7.             $this->_controller=$controller;  
  8.             $controller->init();  
  9.             $controller->run($actionID);  
  10.             $this->_controller=$oldController;  
  11.         }  
  12.         else  
  13.             throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',  
  14.                 array('{route}'=>$route===''?$this->defaultController:$route)));  
  15.     }  
  16.     public function createController($route,$owner=null)  
  17.     {  
  18.         if($owner===null)  
  19.             $owner=$this;  
  20.         if(($route=trim($route,'/'))==='')  
  21.             $route=$owner->defaultController;  
  22.         $caseSensitive=$this->getUrlManager()->caseSensitive;  
  23.         $route.='/';  
  24.         while(($pos=strpos($route,'/'))!==false)  
  25.         {  
  26.             $id=substr($route,0,$pos);  
  27.             if(!preg_match('/^\w+$/',$id))  
  28.                 return null;  
  29.             if(!$caseSensitive)  
  30.                 $id=strtolower($id);  
  31.             $route=(string)substr($route,$pos+1);  
  32.             if(!isset($basePath))  // first segment  
  33.             {  
  34.                 if(isset($owner->controllerMap[$id]))  
  35.                 {  
  36.                     return array(  
  37.                         Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner),  
  38.                         $this->parseActionParams($route),  
  39.                     );  
  40.                 }  
  41.                 if(($module=$owner->getModule($id))!==null)  
  42.                     return $this->createController($route,$module);  
  43.                 $basePath=$owner->getControllerPath();  
  44.                 $controllerID='';  
  45.             }  
  46.             else  
  47.                 $controllerID.='/';  
  48.             $className=ucfirst($id).'Controller';  
  49.             $classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';  
  50.             if(is_file($classFile))  
  51.             {  
  52.                 if(!class_exists($className,false))  
  53.                     require($classFile);  
  54.                 if(class_exists($className,false) && is_subclass_of($className,'CController'))  
  55.                 {  
  56.                     $id[0]=strtolower($id[0]);  
  57.                     return array(  
  58.                         new $className($controllerID.$id,$owner===$this?null:$owner),  
  59.                         $this->parseActionParams($route),  
  60.                     );  
  61.                 }  
  62.                 return null;  
  63.             }  
  64.             $controllerID.=$id;  
  65.             $basePath.=DIRECTORY_SEPARATOR.$id;  
  66.         }  
  67.     }  




forward和redirect的区别显而易见

浏览器url地址

是否支持绝对地址

是否传递参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值