Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 - check if the user is logged in view

Tags:

yii

yii2

I am trying to check is the user logged inside my view file, but I keep getting this error:

Call to undefined method Yii::app()

I tried adding $ before app but the error is still there (this time it is Undefined variable: app). Is it possible to this is view?

This is the code I use the check if the user is logged:

<?php
        if(Yii::app()->isGuest)
            echo 'User is not logged!';
    ?>
like image 363
Sasha Avatar asked Jun 17 '15 17:06

Sasha


People also ask

How to check user is logged in or not in yii2?

Using yii\web\User You can detect the identity of the current user using the expression Yii::$app->user->identity . It returns an instance of the identity class representing the currently logged-in user, or null if the current user is not authenticated (meaning a guest).

What is isGuest in yii2?

You may use $isGuest to determine whether the current user is a guest or not. If the user is a guest, the $identity property would return null . Otherwise, it would be an instance of yii\web\IdentityInterface.


1 Answers

In Yii2 the correct syntax is

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

or

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

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

Hope it helps.

like image 95
Mat Avatar answered Oct 20 '22 22:10

Mat