Problems Redirecting users to View from Component

I’m having issues redirecting users from my component to Login page view. Below is the code with all the solutions I tried…My view file is located in frontend/view/site/login.php


namespace frontend\components;

use Yii;

use yii\base\Behavior;

class VerifyLog extends Behavior



	public function events()


        return [

            \yii\web\Application::EVENT_BEFORE_REQUEST => 'VerifyLog',



    public function VerifyLog()





       //None of these are working


       // return $this->render('login');

       // return $this->goHome();

       //return Yii::$app->view->render('site/login');

       // return Yii::$app->view->render('@app/views/site/login.php');

       //return $this->redirect(['view']);





Also on that note(newbie question) is there any difference btn

1. if(!\Yii::$app->user->isGuest) 

2. if(Yii::$app->user->isGuest)

3. if(\Yii::$app->user->isGuest)

Hi Aeonia,

You are doing the right way… But you have to skip the login controller and action. Your code is actually again will check for guest and the loop continues infinitely.

if(Yii::$app->user->isGuest && Yii::$app->controller->id !== 'site' && Yii::$app->controller->action->id !== 'login'){

    return Yii::$app->getResponse()->redirect(['site/login']);


I hope this will help you…

Cheers! Happy Coding…