您的位置:首页 > 编程语言 > PHP开发

#Yii框架学习之路#YII的forward\redirect

2013-10-31 10:13 337 查看
以下代码都是在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
plaincopy

($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= 4000
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
plaincopy

public function redirect($url,$terminate=true,$statusCode=302)  

    {  

        if(is_array($url))  

        {  

            $route=isset($url[0]) ? $url[0] : '';  

            $url=$this->createUrl($route,array_splice($url,1));  

        }  

        Yii::app()->getRequest()->redirect($url,$terminate,$statusCode);  

    }  

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

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

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

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
plaincopy

/** 

     * Processes the request using another controller action. 

     * This is like {@link redirect}, but the user browser's URL remains unchanged. 

     * In most cases, you should call {@link redirect} instead of this method. 

     * @param string $route the route of the new controller action. This can be an action ID, or a complete route 

     * with module ID (optional in the current module), controller ID and action ID. If the former, the action is assumed 

     * to be located within the current controller. 

     * @param boolean $exit whether to end the application after this call. Defaults to true. 

     * @since 1.1.0 

     */  

[php] view
plaincopy

public function forward($route,$exit=true)  

    {  

        if(strpos($route,'/')===false)  

            $this->run($route);  

        else  

        {  

            if($route[0]!=='/' && ($module=$this->getModule())!==null)  

                $route=$module->getId().'/'.$route;  

            Yii::app()->runController($route);  

        }  

        if($exit)  

            Yii::app()->end();  

    }  

[php] view
plaincopy

public function runController($route)  

    {  

        if(($ca=$this->createController($route))!==null)  

        {  

            list($controller,$actionID)=$ca;  

            $oldController=$this->_controller;  

            $this->_controller=$controller;  

            $controller->init();  

            $controller->run($actionID);  

            $this->_controller=$oldController;  

        }  

        else  

            throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',  

                array('{route}'=>$route===''?$this->defaultController:$route)));  

    }  

    public function createController($route,$owner=null)  

    {  

        if($owner===null)  

            $owner=$this;  

        if(($route=trim($route,'/'))==='')  

            $route=$owner->defaultController;  

        $caseSensitive=$this->getUrlManager()->caseSensitive;  

        $route.='/';  

        while(($pos=strpos($route,'/'))!==false)  

        {  

            $id=substr($route,0,$pos);  

            if(!preg_match('/^\w+$/',$id))  

                return null;  

            if(!$caseSensitive)  

                $id=strtolower($id);  

            $route=(string)substr($route,$pos+1);  

            if(!isset($basePath))  // first segment  

            {  

                if(isset($owner->controllerMap[$id]))  

                {  

                    return array(  

                        Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner),  

                        $this->parseActionParams($route),  

                    );  

                }  

                if(($module=$owner->getModule($id))!==null)  

                    return $this->createController($route,$module);  

                $basePath=$owner->getControllerPath();  

                $controllerID='';  

            }  

            else  

                $controllerID.='/';  

            $className=ucfirst($id).'Controller';  

            $classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';  

            if(is_file($classFile))  

            {  

                if(!class_exists($className,false))  

                    require($classFile);  

                if(class_exists($className,false) && is_subclass_of($className,'CController'))  

                {  

                    $id[0]=strtolower($id[0]);  

                    return array(  

                        new $className($controllerID.$id,$owner===$this?null:$owner),  

                        $this->parseActionParams($route),  

                    );  

                }  

                return null;  

            }  

            $controllerID.=$id;  

            $basePath.=DIRECTORY_SEPARATOR.$id;  

        }  

    }  

原文地址:http://blog.csdn.net/mengxiangbaidu/article/details/6987089       也是csdn的,谢谢原文作者,方便自己查吧..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: