Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 - flash not visible after redirect

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() ?>
like image 676
Joe Avatar asked Oct 02 '14 13:10

Joe


5 Answers

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.

like image 120
user5653303 Avatar answered Nov 15 '22 21:11

user5653303


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.

like image 24
Akmal Avatar answered Nov 15 '22 22:11

Akmal


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,
        ]);
    }
like image 39
Mihai P. Avatar answered Nov 15 '22 22:11

Mihai P.


Add return in your redirect

Yii::$app->session->getFlash('key', 'message');

return $this->redirect(['yourAction']);
like image 41
leonardo avella Avatar answered Nov 15 '22 23:11

leonardo avella


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.
like image 40
HeadHunter Avatar answered Nov 15 '22 22:11

HeadHunter