MeV
MeV

Reputation: 3958

yii2 - redirect from view to another view

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()

Upvotes: 6

Views: 12379

Answers (2)

soju
soju

Reputation: 25312

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']));

Upvotes: 17

ScaisEdge
ScaisEdge

Reputation: 133360

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']));

Upvotes: 1

Related Questions