Yii2 - check if user entered view

I'm trying to check if a user is logged in inside my viewfile, but I keep getting this error:

Call to undefined method Yii::app()

      

I tried adding $ before the app , but the error still exists (this time it's Undefined variable: app ). Is this viewing possible?

This is the code where I am using validation if the user is logged in:

<?php
        if(Yii::app()->isGuest)
            echo 'User is not logged!';
    ?>

      

+3


source to share


2 answers


In Yii2, the correct syntax is

Yii::$app->user->getIsGuest();

      

or



Yii::$app->user->isGuest;

      

Have a look at the documentation for more details: http://www.yiiframework.com/doc-2.0/yii-web-user.html

Hope it helps.

+16


source


In yii2, you need to define app () with $ sign as $ app ().



<?php
       if(Yii::$app->user->isGuest){
        echo 'User is not logged!';
       }
?>

      

0


source







All Articles