Flash messages seems to be broken in case of redirecting. I made simple test code:
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', []);
}
I call the action without parameter, it adds a flash and redirects. When it renders the page - flash is not present.
The view part is fine, because if I set flash and make a render without redirect it is rendered properly.
Why?
EDIT: Layout view code:
<?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() ?>
I've gotten the same error until I found out that the return
is missing in my code. So, with return $this->redirect()
it works just fine and with $this->redirect
it does not work well.
In my case flash messages weren't available when I was redirecting from beforeAction
method. And using Yii::$app->end()
really helped me. Here is my code:
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;
}
Hope it'll help someone.
Your code looks ok, I am not sure what the problem is. You can try using
return $this->redirect(['test', 'test' => 1]);
Instead of
return Yii::$app->getResponse()->redirect(array('test', 'test' => 1));
This is how most Yii examples are. But your code looks ok after looking at http://www.yiiframework.com/doc-2.0/yii-web-response.html#redirect()-detail
Are you sure your session is working properly and you are not destroying it at any point?
This works for me:
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,
]);
}
Add return
in your redirect
Yii::$app->session->getFlash('key', 'message');
return $this->redirect(['yourAction']);
I have a solution: You can add the line below in the view file where the message is to appear:
Then you can add the line below in the view file, where the message is supposed to appear.
if(Yii::$app->getResponse()->getStatusCode() != 302) {
Yii::$app->session->getFlash('error');
}
Or alternatively, you can add the line below in the content layout
<?= Alert::widget() ?>
// Before the line
<?= $content ?>
// in app/views/layouts/_content.php
// Depending on how you arranged your files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With