0

I use Yii2 and I have this problem. This is my Update action:

public function actionUpdate($id)
    {
      $searchModel = new AutoriSearch();
      $searchModel1 = new EditoriSearch();
      $searchModel2 = new LuoghiSearch();
      $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
      $dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams);
      $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->IDBibliografia]);
        }

        return $this->render('update', [
            'model' => $model,
            'dataProvider' => $dataProvider,
            'dataProvider1' => $dataProvider1,
            'dataProvider2' => $dataProvider2,
            'searchModel' => $searchModel,
            'searchModel1' => $searchModel1,
            'searchModel2' => $searchModel2
        ]);
    }

I want, after saving, to return to the previous page. So the problem is in redirect raw. So, I click the button from the index, to go to the update action. Then, when I save the update, I want to return to index (but it need to be the same previous page with the same previous Url). I found various solution on Stack, but anything can help me. I need help, thank you!!!!

1

2 Answers 2

1

try this:

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

maybe duplicate of Yii2 redirect to previous page

if you need to capture the url before previous, you might use yii2's sessions to store the referrer in actionUpdate like so (e.g. to capture search params and page no from index):

Yii::$app->session->set('url', Yii::$app->request->referrer ?: Yii::$app->homeUrl);

and redirect after save:

return $this->redirect(Yii::$app->session->get('url', ['view', 'id' => $model->IDBibliografia]));
2
  • Sorry, this is not the solution. This return to the index, but at the first page. I want to come back to the previous page. For example, if I activate the update action from the index page 4, I want, after saving, to come back at the page 4. Commented Apr 8, 2021 at 6:43
  • 1
    that's what i tried to explain. store the referrer (e.g. index/?page=4) in the session within the update action (the action, which renders the form, and not stores the data). so if you update item #101 from page #4 and redirect to your edit form, remember the referrer for later use.
    – e-frank
    Commented Apr 9, 2021 at 1:36
0

You could use Yii::$app->request->referrer which returns the last page the user was on.

Usage is straightforward:

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

You need also take into account that referrer can be null:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.