V
V
Vladimir2022-04-21 16:55:00
Yii
Vladimir, 2022-04-21 16:55:00

How to redirect yii to the previous page?

There is a page comp/computer?id=15
on it there are reviews that can be edited . comp/computer/update?id=2
I want that after I changed the review. = return again to this page with the product.
If I write directly in the forehead in this way, then I will return, exactly to 15. But it seems to me that I need to somehow get id from here $computers = addComputer::findOne($id); = but if you use it in actionUpdate = then Unused local variable 'computers'. The value of the variable is not used anywhere.
return $this->redirect(["?id=15"]);

public function actionUpdate($id)
  {
    $model = new ReviewForm();
    $comment = Review::findOne($id);
    $computers = addComputer::findOne($id);  
    // если пришли post-данные
    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'));
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2022-04-22
@faradeifrontend

Should have changed

<?= Html::a('Редактировать', Url::to(['computer/update', 'id' => $comment->id])) ?>

on the
<?= Html::a('Редактировать', Url::to(['computer/update', 'id' => $comment->id, 'cid'=>$computers['id'] ])) ?>

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

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

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

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

      Yii::$app->session->setFlash(
        'success',
        'Success'
      );
    }

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question