对Yii2中 yii\web\User的理解,和自建的app\models\User(基础版),frontend\models\User的应用原理
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的应用原理的更多相关文章
- Yii2 中cookie的用法(2)
设置Cookie PHP setcookie("name", "Larry", time()+3600); Yii2 $cookies = Yii::$app- ...
- yii 基础版用rbac-plus
1.将高级版的common/models/user.php覆盖掉基础版的models/user.php 2.将命名空间 namespace common\models;改为 namespace app ...
- Yii2 使用json 和设置component 中'format' => yii\web\Response::FORMAT_JSON 的区别
在Yii2中如果设置了 'response' => [ 'format' => yii\web\Response::FORMAT_JSON, 'charset' => 'UTF- ...
- 解决yii2中 Class yii/web/JsonParser does not exist, ReflectionException问题
最近在调试RESTful API示例时,出现以下错误: { "name": "Exception", "message": "Cl ...
- YII2中behavior行为的理解与使用
YII2中的行为说白了就是对组件功能的扩展,在不改变继承关系的条件下. 行为附加到组件后,行为将注入自已的方法和属性到组件,可以像组件访问自定义的方法和属性一样访问行为. 注意行为是对功能的扩展,不要 ...
- Yii2.0 安装使用报错:yii\web\Request::cookieValidationKey must be configured with a secret key.
下载了Yii2.0的basic版,配置好apache之后,浏览器访问,出现如下错误: Invalid Configuration – yii\base\InvalidConfigException y ...
- yii2中的别名路径,@webroot , @web
定义在yii\web\Application 的bootstrap中, Yii::setAlias('@webroot', dirname($request->getScriptFile())) ...
- yii2安装配置完成后,网页打开报错yii\web\Request::cookieValidationKey must be configured with a secret key
下载了Yii2.0的basic版,配置好nginx之后,浏览器访问,出现如下错误: Invalid Configuration – yii\base\InvalidConfigException yi ...
- Yii2中的零碎知识点
PHP最佳实践 1 PHP获取时间戳:echo time(); 时间戳转换 date('Y-m-d H:i:s', $时间戳); 2 linux 显示命令 ls 显示所有文件夹 查看命令:tail ...
随机推荐
- webApi2 结合uploadify 上传报错解决办法
报错代码: Error reading MIME multipart body part. 处理办法: <httpRuntime targetFramework=" />
- Java适配器模式的简单应用
对于刚从工厂生产出来的商品,有些功能并不能完全满足用户的需要.因此,用户通常会对其进行一定的改装工作.编写程序为普通的汽车增加GPS定位功能,借此演示适配器模式的用法. 思路分析: 这个问题的需求是, ...
- SpringMVC由浅入深day01_9商品修改功能开发
9 商品修改功能开发 9.1 需求 操作流程: 1.进入商品查询列表页面 2.点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询) 要修改的商品从数据库查询,根据商品id(主键)查询商 ...
- 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式
7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...
- 简单思考 如何用PHP输出本周的周三日期
简单思考 如何用PHP输出本周的周三日期?比如今天2017-10-31 计算本周周三应该是2017-11-01 自己动脑筋想一想,然后给我留言或者评论
- 错误 error C2678: 二进制“<”: 没有找到接受“const card”类型的左操作数的运算符(或没有可接受的转换)
错误出现的地方如下 而我又重载了<运算符,但是我没有将<运算符重载函数定义成const类型,此处是const _Ty&,不可以调用非const成员函数 而且,一般而言,像<, ...
- 把mongodb服务添加到系统服务中,报错:[sc] openscmanager 失败 5
添加mongodb系统服务命令如下: sc create MongoDB binPath= "D:\MongoDB\bin\mongod.exe --service --dbpath D:\ ...
- python线程池(threadpool)
一.安装 pip install threadpool 二.使用介绍 (1)引入threadpool模块 (2)定义线程函数 (3)创建线程 池threadpool.ThreadPool() (4)创 ...
- [Linux] 修改用户名密码
1. 普通用户或root用户修改自身登录密码:在终端使用passwd命令. linaro@linaro-ubuntu-desktop:~$ passwd Changing password for l ...
- Push rejected: Push master to origin/master was rejected /failed to push some refs to /git did not exit cleanly
用studio提交代码报 Push rejected: Push master to origin/master was rejected 用TortiuseGit提交代码报下面错,(我是用这种方法解 ...