Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 - redirect from view to another view

Tags:

php

yii

yii2

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

like image 994
MeV Avatar asked Apr 08 '16 16:04

MeV


2 Answers

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']));
like image 110
soju Avatar answered Nov 11 '22 13:11

soju


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']));
like image 32
ScaisEdge Avatar answered Nov 11 '22 14:11

ScaisEdge