For those who still struggling with returning to previous page: You have to create a property in login model $referer
, and at the time when you initialize your Login
model you set this property to Yii::$app->request->referrer
. You have to go this way because after you submit your form your referrer does change to current page, and since it is null
, it return you to index action. So the way how to pass the return URL is to store it in the hidden field at the form, so when you submit the form you have this URL loaded and you can perform action to return $this->goBack($form->referer ? $form->referer : null)
.
Here is the code:
Login model:
public $referer;
Controller action:
$model = new Login();
$model->referer = Yii::$app->request->referrer;
...
if($model->load(Yii::$app->request->post()){
...
if($model->save()){
return $this->goBack((($model->referer) ? $model->referer : null));
}
}
View login.php:
<?= $form->field($model, 'referer')->hiddenInput()->label(false) ?>