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!';
?>
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With