6
votes

I am trying to redirect from a View to another View and I cannot find a solution online.

I have tried using:

Yii::$app->request->redirect(Yii::$app->createAbsoluteUrl("site/view"));

But I receive the following error:

Unknown Method – yii\base\UnknownMethodException

Calling unknown method: yii\web\Application::createAbsoluteUrl()

2
But if I use Yii::$app->response->redirect('site/hat'); my url is changing to /web/site/hat which does not exist. The right url is index.php?r=site/hat - MeV
@soju thanks so much! - MeV

2 Answers

15
votes

You should use response instead of request :

Yii::$app->response->redirect(['site/view']);

You can also use Url helper to get an absolute url :

Yii::$app->response->redirect(Url::to(['site/view'], true));

And if you want to use createAbsoluteUrl() :

Yii::$app->response->redirect(Yii::$app->urlManager->createAbsoluteUrl(['site/view']));
1
votes

If you use $app then use it always use

Yii::$app->request->redirect(Yii::$app->createAbsoluteUrl("site/hat"));

instead of

Yii::$app->request->redirect(Yii::app()->createAbsoluteUrl("site/hat"));

or you can use this for get the url

Yii::$app->request->redirect(['site/hat']));