Hi There,
I am facing issue with $this->redirect function its not redirecting to suggested location.
<?php
namespace backend\controllers;
use Yii;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use mdm\admin\models\form\Signup;
use backend\components\BackendController;
use yii\helpers\Url;
class SiteController extends BackendController
{
public function behaviors()
{
return [
"access" => [
"class" => AccessControl::className(),
"rules" => [
[
"actions" => ["login", "error", "signup", "index"],
"allow" => true,
],
[
"actions" => ["logout"],
"allow" => true,
"roles" => ["@"],
],
],
],
"verbs" => [
"class" => VerbFilter::className(),
"actions" => [
"logout" => ["post"],
],
],
];
}
public function actions()
{
return [
"error" => [
"class" => "yii\web\ErrorAction",
],
];
}
public function actionIndex()
{
if (Yii::$app->user->isGuest) {
return $this->redirect(Url::toRoute(["site/login"], true));
}
return $this->render("index");
}
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$this->layout = "login";
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render("login", [
"model" => $model,
]);
}
}
}
Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:
Thanks!
_This is an automated comment, triggered by adding the label status:need more info._
return $this->redirect(Url::toRoute(["site/login"], true));
Above mentioned redirect code is not working for me.
I am facing this issue on every redirect.
Due to this issue, I can't set any controller/action redirection in YII2
@navphpdeveloper what do you mean by not working? What exactly the $this->redirect(Url::toRoute(["site/login"], true)) returns? Is an exception being thrown while this call?
Url::to is not needed here, because of https://github.com/yiisoft/yii2/blob/master/framework/web/Controller.php#L202
Common way to do redirect to same site is
return $this->redirect(['site/login']);
It works perfectly for me.
return $this->redirect("https://www.github.com");
Error
Call to a member function redirect() on string
vendor/yiisoft/yii2/web/Controller.php at line 240
php 7.1.32
yii 2.0.38
@derek778 this was already mentioned in issues - request and response properties since 2.0.36 are configurable and instantiated in controller by default so if you extend controller and use init() there make sure you call parent::init() inside as you should always.
Most helpful comment
@navphpdeveloper what do you mean by
not working? What exactly the$this->redirect(Url::toRoute(["site/login"], true))returns? Is an exception being thrown while this call?