Yii2 - check if the user is logged in view

淺唱寂寞╮ 提交于 2019-12-22 04:08:44

问题


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!';
    ?>

回答1:


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.




回答2:


In yii2 you have to define the app() with $ sign as $app().

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


来源:https://stackoverflow.com/questions/30898214/yii2-check-if-the-user-is-logged-in-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!