简体   繁体   English

从控制器Yii会话重定向后无法正常工作

[英]After redirect from a controller Yii session not working

After save a model i want to store a model auto increment id in a session variable and redirect to a view page. 保存模型后,我想在会话变量中存储模型自动增量ID并重定向到视图页面。 When i want to echo that session variable in view page it is not working. 当我想在视图页面中回显该会话变量时,它不起作用。

Note: I don't use user login/logout system. 注意:我不使用用户登录/注销系统。

In config.php 在config.php中

'session' => array(
        'autoStart'=>true,
        'timeout'=>1200,
    ),

In Controller File 在控制器文件中

if ($model->save()) {
    Yii::app()->session['orderId']=$model->id;
    $this->redirect(array('view', 'id' => $model->id));
}

In the controller where i use redirect() if i use render() then it's work. 在控制器中,如果我使用render(),我将使用redirect(),这是可行的。 After redirect it's not working. 重定向后,它不起作用。

In View File 在查看文件中

echo Yii::app()->session['orderId'];

Please Help Me :( 请帮我 :(

You can store session data with 您可以使用存储会话数据

if ($model->save()) {
Yii::app()->user->setState('orderId', $model->id);
}

Get it with 用它

echo Yii::app()->user->getState('orderId');

Try this , 尝试这个 ,

in your controller 在您的控制器中

if ($model->save()) {
    Yii::app()->user->setState("orderId",$model->id);
     $this->redirect(array('view', 'id' => $model->id));

}

//get session variable //获取会话变量

Yii::app()->user->getState("orderId");

尝试在重定向之前添加“返回”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM