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. 缓存管理(本地缓存+memcached)

    http://www.cnblogs.com/daizhj/archive/2009/11/17/1604436.html

  2. 爬豆瓣影评,记下解决maximum recursion depth exceeded in cmp

    #主要是爬取后给别人做自然语言分析,没其他意思. #coding=utf8 import requests,re from lxml import etree import sys reload(sy ...

  3. Laravel5.1 填充数据库

    当我们创建好表结构后 通常都要生成一些测试用的数据来测试,应对这个场景呢 Laravel提供了相当好的服务 --seed Laravel的seeder都会放在:/database/seeders 目录 ...

  4. NHibernate 数据查询之Linq to NHibernate

    刚学NHibernate的时候觉得,HQL挺好用的,但是终归没有与其他技术相关联,只有NHibernate用到,一来容易忘记,二来没有智能提示,排除错误什么的都不给力,直到看到一个同事用Linq to ...

  5. jquery获取元素颜色css('color')的值返回RGB

    css代码如下: a, a:link, a:visited { color:#4188FB; } a:active, a:focus, a:hover { color:#FFCC00; } js代码如 ...

  6. 九度 1254:N皇后问题

    Leetcode 原题. 这里 N 最大会取到 13, TLE 了 代码 #include <iostream> #include <stdio.h> using namesp ...

  7. C#索引器理解

    C#索引器介绍举例 索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 ( ...

  8. centos 上不了网了

    昨天还用的好好的,今天就上不了网了,郁闷,不过,正好是一次学习linux网络配置的好机会,这会已经把它折腾好了,此文就是在linux下面的浏览器中写的! 先检查一下虚拟机中的网络设置是否正常,由于我的 ...

  9. c++的字节对齐

    win32平台下的微软C编译器对齐策略: 1)结构体变量的首地址能够被其最宽数据类型成员的大小整除.编译器在为结构体变量开辟空间时,首先找到结构体中最宽的数据类型,然后寻找内存地址能被该数据类型大小整 ...

  10. U3D调用7z解压文件

    using UnityEngine; using System; using System.IO; using System.Diagnostics; public class Test : Mono ...