Yii2 - flash not visible after redirect

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.


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']);