Php 我不工作了

Php 我不工作了,php,yii,url-redirection,Php,Yii,Url Redirection,在Yii框架中,$this->重定向功能不工作 class TplUserNavigation extends CWidget{ public function run() { if(isset(Yii::app()->user->id) && Yii::app()->user->getState('userType') == 'User'){ $users = Users::model()->findBy

在Yii框架中,
$this->重定向
功能不工作

class TplUserNavigation extends CWidget{

public function run()
    {
        if(isset(Yii::app()->user->id) && Yii::app()->user->getState('userType') == 'User'){
         $users = Users::model()->findByAttributes(array('id'=>Yii::app()->user->id));
         $this->render('webroot.themes.'.Yii::app()->theme->name.'.views.layouts.tpl_navigation', array('users'=>$users));
        }else{
          $this->redirect('site/index'); 
    }
}
尝试:

或者,如果要重定向到主页:

$this->redirect(Yii::app()->homeUrl);
CWidget尝试:

$this->owner->redirect(array("site/index"));

阅读更多关于小部件重定向的信息。

从重定向功能中删除数组

Syntex
$this->重定向(/)

否则试试这个

$this->redirect(Yii::app()->createUrl('controller/action'));
这是一种简单的方法(将代码放在返回函数和exit()之前):

你可以用

$this->redirect(Router::url(array('controller'=>'site', 'action'=>'index')));

由于您只是想访问站点的索引,请尝试以下操作:

 $this->redirect(Yii::app()->baseUrl);
如果要将用户重定向到主页以外的其他页面,请尝试使用:

 $this->redirect(Yii::app()->createUrl('controlller/action'));

当您尝试使用yii url规则重写url时,这将非常方便

您可以这样使用:

$this->redirect(数组('action_name'))

示例:$this->重定向(数组('view')

试试这个:
$this->重定向(数组(“站点/索引”)

您还可以使用以下方法在Yii中重定向

Yii::app()->getController()->redirect(array('/path/to/url'));
那么是Yii 1:

e、 g

阅读手册:
你应该试试这个,希望能有所帮助

$this->redirect(array('site/index'));
你也可以试试这个

Yii::app()->getController()->redirect(array('/path/to/url'));
您不能使用:

$this->redirect 
在widget中,因为在这种情况下,$这是用于当前widget的,widget没有重定向方法,也没有控制器。您需要的是Yii::app()->controller,它将从Yii中的任何位置获取活动控制器实例,例如:

Yii::app()->controller->redirect('site/index');
可能是这样的(取决于您的项目),您必须使用:


Yii::$app->redirect('site/index')
来自控制器。

在您的情况下,
$this
是一个,而不是一个。CWidget没有重定向方法

其次,
$this->createUrl
将创建一个与当前控制器相关的URL(
$this->owner

使用
Yii::app()->createUrl('/site/index')

注意
站点
之前的前导
/
创建URL
相对于
基本URL
,而不是当前控制器)。

1)重定向到任何地方

2)在控制器之间重定向


我已经试过了,但错误是:重定向没有关闭。我正在组件中使用这个函数。@aman你试过了吗<代码>$this->所有者->重定向(数组(“站点/索引”)@YatinMistry:返回此小部件的所有者/创建者->基本控制器解释:redirect()是CController类的函数,因此它只能由CController的对象调用,不能直接由CWidget的对象调用。在您的情况下,$this->owner是控制器类的对象,在其他情况下,您可以使用Yii::app()->getController()->重定向(数组('controller/action');希望这有助于理解流程。我已经尝试过了,但它的错误是:redirect没有closeri try,但是,错误是:TplUserNavigation及其行为没有一个名为“redirect”的方法或闭包。目前这是一个非常简短且低质量的答案。请详述你的答案,并说明为什么你的答案是正确的。
    $this->redirect(['route_to_action,GET_PARAMETERS]);
$this->redirect(array('site/index'));
Yii::app()->getController()->redirect(array('/path/to/url'));
$this->redirect 
Yii::app()->controller->redirect('site/index');
Yii::app()->request->redirect(Yii::app()->createAbsoluteUrl("controller/action"));
 $this->redirect(array("controller/action"));