Search code examples
yiiyii2

Yii2 redirect to previous page after update review


I have a page comp/computer?id=15

  • it has reviews that can be edited through link http://comp/computer/update?id=3 = with FORM and submit button
  • how to go back after sumbit

    public function actionUpdate($id)
    {
        $model = new ReviewForm();
        $comment = Review::findOne($id);

        if ($model->load($this->request->post())) {

            $comment->text = $model->text;
            if ($comment->save(false)) {

                return $this->redirect(["?id=15"], );  ????????????
            }
        
            Yii::$app->session->setFlash(
                'success',
                'Success'
            );
        }

        $model->setAttributes($comment->getAttributes(['name', 'email', 'text']));
        return $this->render('update', compact('model'));
    }


Solution

  • simply use referrer.

    return $this->redirect(Yii::$app->request->referrer)

    If it has no referrer or link open directly then you should either pass computer_id as param or you must have computer_id as foreign key in your review table.

    Let say you have relationship with review and computer table. then you can use like this.

    $compId = $comment->computer_id; // or 15 or you can paas param here

    return $this->redirect(["comp/computer", "id"=> $compId]);

    if comp is your hostname then

    return $this->redirect(["computer", "id"=> $compId]);

    its should be controller/action

    return $this->redirect(["controllerId/actionId", "id"=> $compId]);

    Send via mobile, sorry for typos.