After switching from yii to yii2, I found a problem: if I installed flash-message and redirected to the controller, then the flash-message will not appear on the new page. How to fix it?

    1 answer 1

    $this->redirect() does not stop execution.

    Consider an example of a simple action in the first version of yii :

     public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::app()->request->isPostRequest) && $model->save()) { $this->redirect(['view', 'id' => $model->id]); } else { $this->render('update', [ 'model' => $model, ]); } } 

    And for yii2 :

     public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'model' => $model, ]); } } 

    At first glance, the difference is not noticeable, but it is. Please note that we have added the keyword return in the code. Both before render and before redirect .

    In fact, we now make a stop in action with our hands.

    Do not forget to put return and everything will be fine.