php - Yii2 setFlash not work or lost if redirect
Get the solution ↓↓↓I want to set flash message, but flash message always missing. the flow of my controller like this:
public function actionOne()
{
Yii::$app->session->setFlash('error', 'message text');
return $this->redirect(['two', 'param1' => 'value1']);
}
public function actionTwo()
{
return $this->render('view');
}
And on the file view.php
<?php
$flashes = Yii::$app->session->getAllFlashes();
var_dump($flashes);
?>
I always get empty. How I can show flash message after redirect?
Answer
Solution:
In case when you redirect to another action - You have to pass third parameterfalse
. Link to documentation: setFlash()
Yii::$app->session->setFlash('error', 'message text', false);
In other cases, when you want to show the message in the same action you can use it like this:
Yii::$app->session->setFlash('error', 'message text'); // default third parameter is set to true.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: uncaught mysqli_sql_exception
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.