Yii2: $this->redirect() not redirecting

Created on 28 Jul 2017  路  6Comments  路  Source: yiisoft/yii2

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,
            ]);
        }
    }
}
need more info

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?

All 6 comments

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

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.

Was this page helpful?
0 / 5 - 0 ratings