Php Yii2-重定向后不可见闪烁
在重定向的情况下,Flash消息似乎被破坏。我编写了简单的测试代码:Php Yii2-重定向后不可见闪烁,php,redirect,yii2,flash-message,Php,Redirect,Yii2,Flash Message,在重定向的情况下,Flash消息似乎被破坏。我编写了简单的测试代码: public function actionTest($test = 0) { if($test == 0) { Yii::$app->getSession()->addFlash('success', 'Follow the white rabbit'); return Yii::$app->getResponse()->redirect(array('test
public function actionTest($test = 0) {
if($test == 0) {
Yii::$app->getSession()->addFlash('success', 'Follow the white rabbit');
return Yii::$app->getResponse()->redirect(array('test', 'test' => 1));
}
return $this->render('test', []);
}
我调用这个没有参数的操作,它会添加一个flash并重定向。当它呈现页面时-flash不存在
视图部分很好,因为如果我设置flash并在不重定向的情况下进行渲染,它将正确渲染
为什么?
编辑:
布局视图代码:
<?php
use frontend\widgets\Alert;
$this->beginPage();
echo $this->render('partials/head');
?>
<body class="no-sidebar">
<?= $this->beginBody() ?>
<div id="header">
<?= $this->render('partials/top') ?>
<?= $this->render(Yii::$app->user->isGuest ? 'menus/guest' : 'menus/registered') ?>
</div>
<!-- Main -->
<div id="main">
<?= Alert::widget() ?>
<?= $content ?>
</div>
<?= $this->render('partials/footer') ?>
<?= $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
您的代码看起来不错,我不确定问题出在哪里。你可以尝试使用
return $this->redirect(['test', 'test' => 1]);
而不是
return Yii::$app->getResponse()->redirect(array('test', 'test' => 1));
这就是大多数Yii示例的方式。但是你的代码在看了
您确定您的会话工作正常,并且在任何时候都没有破坏它吗
这对我很有用:
public function actionChangeDetails()
{
$model = Contact::findOne(Yii::$app->user->identity->id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', 'Form Saved');
return Yii::$app->getResponse()->redirect(['my-account/change-details']);
}
return $this->render('changeDetails', [
'model' => $model,
]);
}
在查看页面上,您应该添加:“session->getFlash('success');?>”就像这里所描述的:,然后您将看到您的flash消息我得到了相同的错误,直到我发现我的代码中缺少返回的
。因此,使用return$this->redirect()
它工作得很好,而使用$this->redirect它工作得不好。我有一个解决方案:
您可以在要显示消息的视图文件中添加以下行:
然后,您可以在视图文件中添加下面的行,即消息应该出现的位置
if(Yii::$app->getResponse()->getStatusCode() != 302) {
Yii::$app->session->getFlash('error');
}
或者,也可以在内容布局中添加下面的行
<?= Alert::widget() ?>
// Before the line
<?= $content ?>
// in app/views/layouts/_content.php
// Depending on how you arranged your files.
//排队前
//在app/views/layouts/_content.php中
//取决于你如何安排你的文件。
在重定向中添加返回
Yii::$app->session->getFlash('key', 'message');
return $this->redirect(['yourAction']);
在我的例子中,当我从beforeAction
方法重定向时,flash消息不可用。使用Yii::$app->end()
真的帮助了我。这是我的密码:
public function beforeAction($action) {
if ($someVariableIs === false) {
Yii::$app->session->addFlash("negative", "My flash message");
Yii::$app->getResponse()->redirect(["/path/to/redirect"]);
Yii::$app->end();
}
// some of your code...
return true;
}
希望它能帮助别人。Simple$this->redirect
可以工作(我在新的Yii2项目中测试了它),但在我的情况下我不能这样做,因为我必须返回Response对象。我在yii2github上问过他们,他们无法复制。也许这是某种会话问题,但目前很难找到。我肯定不会在任何地方破坏会话。请使用最新的Yii进行尝试,也许您没有更新的Yii。不,我使用composer创建了新的应用程序。$this->redirect
实际调用Yii::$app->getResponse()->重定向(Url::to($Url),$statusCode)代码>因此这两个函数之间没有区别。但我可以证实这个问题,我自己在我的应用程序上测试过,它完全符合你的描述。你能给我一个你已经做过的bug报告,这样我就可以再次提出这个问题了吗?我在主要答案中添加了代码,就像它对我有用一样。我在我的应用程序上测试了它,效果很好。我个人通常不使用returnyii::$app->getResponse()->重定向(['my-account/change details'])代码>因为这没有意义。这很有意义。如果在不返回的情况下使用重定向,脚本将继续执行,它将呈现页面,用完闪烁,然后重定向。结果是下一页缺少一个闪光灯。这是正常的行为,你救了我。我花了几个小时来处理这个问题,直到我读到你的评论。马上修好了。非常感谢。欢迎来到堆栈溢出!你能解释一下你的答案是如何解决问题的吗?只有代码的答案不是很有用,特别是对于那些偶然发现这篇文章的读者来说。谢谢