Yii2 - 检查用户是否登录视图 [英] Yii2 - check if the user is logged in view

查看:31
本文介绍了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()

我尝试在 app 之前添加 $ 但错误仍然存​​在(这次是 未定义变量: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!';
    ?>

推荐答案

在 Yii2 中正确的语法是

In Yii2 the correct syntax is

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

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

查看文档了解更多详情:http://www.yiiframework.com/doc-2.0/yii-web-user.html

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

希望能帮到你.

这篇关于Yii2 - 检查用户是否登录视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆