yii\web\User 是一个统称,为用户,没有具体实例,只能管理;

此处以app\models\User为基准;

app\models\User 是映射数据表user的model类,同时也实现接口,yii\web\IdentityInterface,为什么要实现这个接口呢,

是因为在yii\web\User 中的login方法:public function login(IdentityInterface $identity, $duration = 0) 中的$identity 要求的类型就是IdentityInterface。

因此在lognForm中实现登陆的时候最后要调用yii\web\User 的方法,

而yii\web\User是组件components里面配置的, 所以要在其他代码中会用到Yii::$app->user,当你登陆以后,这个Yii::$app->user的属性就有值了,

关于 Yii::$app->user 的几个属性, 要查看yii\web\User 中的源码,摘抄一部分如下:

* @property string|int $id The unique identifier for the user. If `null`, it means the user is a guest.
* This property is read-only.
* @property IdentityInterface|null $identity The identity object associated with the currently logged-in
* user. `null` is returned if the user is not logged in (not authenticated).
* @property bool $isGuest Whether the current user is a guest. This property is read-only.
* @property string $returnUrl The URL that the user should be redirected to after login. Note that the type
* of this property differs in getter and setter. See [[getReturnUrl()]] and [[setReturnUrl()]] for details.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class User extends Component
{

其中有@property后面的变量就是Yii::$app->user , 可以写代码如:

Yii::$app->user->id ,  返回值 整数|null      整数表示登陆后的用户id ,返回null表示是游客Guest;

Yii::$app->user->identity , 返回值  实现IdentityInterface类型对象|null      实现IdentityInterface接口的为 app\models\User ,因此此处为app\models\User的对象

Yii::$app->user->isGuest , 返回值 true|false   true表示是游客,false表示不是游客

Yii::$app->user->returnUrl , 返回值  字符串(string)   即跳转到登陆界面之前的链接,(目前是这样理解,不知道对错)

对Yii2中 yii\web\User的理解,和自建的app\models\User(基础版),frontend\models\User的应用原理的更多相关文章

  1. Yii2 中cookie的用法(2)

    设置Cookie PHP setcookie("name", "Larry", time()+3600); Yii2 $cookies = Yii::$app- ...

  2. yii 基础版用rbac-plus

    1.将高级版的common/models/user.php覆盖掉基础版的models/user.php 2.将命名空间 namespace common\models;改为 namespace app ...

  3. Yii2 使用json 和设置component 中'format' => yii\web\Response::FORMAT_JSON 的区别

    在Yii2中如果设置了 'response' => [  'format' => yii\web\Response::FORMAT_JSON,  'charset' => 'UTF- ...

  4. 解决yii2中 Class yii/web/JsonParser does not exist, ReflectionException问题

    最近在调试RESTful API示例时,出现以下错误: { "name": "Exception", "message": "Cl ...

  5. YII2中behavior行为的理解与使用

    YII2中的行为说白了就是对组件功能的扩展,在不改变继承关系的条件下. 行为附加到组件后,行为将注入自已的方法和属性到组件,可以像组件访问自定义的方法和属性一样访问行为. 注意行为是对功能的扩展,不要 ...

  6. Yii2.0 安装使用报错:yii\web\Request::cookieValidationKey must be configured with a secret key.

    下载了Yii2.0的basic版,配置好apache之后,浏览器访问,出现如下错误: Invalid Configuration – yii\base\InvalidConfigException y ...

  7. yii2中的别名路径,@webroot , @web

    定义在yii\web\Application 的bootstrap中, Yii::setAlias('@webroot', dirname($request->getScriptFile())) ...

  8. yii2安装配置完成后,网页打开报错yii\web\Request::cookieValidationKey must be configured with a secret key

    下载了Yii2.0的basic版,配置好nginx之后,浏览器访问,出现如下错误: Invalid Configuration – yii\base\InvalidConfigException yi ...

  9. Yii2中的零碎知识点

    PHP最佳实践 1 PHP获取时间戳:echo  time(); 时间戳转换 date('Y-m-d H:i:s', $时间戳); 2 linux 显示命令 ls 显示所有文件夹 查看命令:tail ...

随机推荐

  1. PHP对HTML代码尸体编码2个函数

    1.htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体. 2.htmlentities() 函数把字符转换为 HTML 实体. 记录下

  2. 用plsql 导入导出oracle表结构数据

    一.导出 (1)导出数据 进入pl/sql,"工具"---->"Export Tables...",然后在弹出的对话框中选择要导出的表,最后点击" ...

  3. FileSaver.js 浏览器导出Excel文件

    限制一:不同浏览器对 blob 对象有不同的限制 具体看看下面这个表格(出自 FileSaver.js): Browser Constructs as Filenames Max Blob Size ...

  4. Java动态调用类中方法

    在Java中,调用类的方法有两种方式:对于静态方法可以直接使用类名调用,对于非静态方法必须使用类的对象调用.反射机制提供了比较另类的调用方式,可以根据需要指定要调用的方法,而不必在编程时确定.调用的方 ...

  5. SpringMVC -- 梗概--源码--贰--异常管理

    附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...

  6. 在oracle配置mysql数据库的dblink

    本文介绍如何在oracle配置mysql数据库的dblink:虽然dblink使用很占资源:俗称“性能杀手”.但有些场景不得不使用它.例如公司使用数据库是oracle:可能其他部门或者CP合作公司使用 ...

  7. java serialize 浅谈

    对象的串行化(Serialization) 一.串行化的概念和目的 1.什么是串行化 对象的寿命通常随着生成该对象的程序的终止而终止.有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复.我们把 ...

  8. 第7章 Ajax数据爬取

    Ajax 简介 Ajax 分析方法 Ajax 结果提取

  9. Unity弹出MessageBox

    [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = Char ...

  10. Flask-Script应用实例

    #coding=utf-8 #Flask-Script是一个可以在flask应用外部编写脚本的扩展 #常用功能: #运行一个开发的服务器 #python shell中操作数据库看 #命令行任务 #fr ...