对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 ...
随机推荐
- 判断一个字符串同时出现几个字符的C#版本和JS版本
C#版本,参考:http://www.cnblogs.com/dudu/p/3841256.html JS版本是我群友写的: var str="abcdef"; var arr=[ ...
- golang interface的使用和实现(翻译整理)
https://blog.csdn.net/u011409801/article/details/79291221
- [Scikit-learn] 1.1 Generalized Linear Models - Lasso Regression
Ref: http://blog.csdn.net/daunxx/article/details/51596877 Ref: https://www.youtube.com/watch?v=ipb2M ...
- Images.xcassets 的简单使用
总结起来Images.xcassets 就这么几句话 (1)以下開始简单整理 进入images.xcassets 右键--->New Image Set 或者点XCode的images.xcas ...
- 让树莓派自动上报IP地址到邮箱,二代B
由于我使用树莓派的场景大多数是在没有显示器.只用terminal连接它的情况下,所以,它的IP地址有时会在重启之后变掉(DHCP的),导致我无法通过terminal连接上它.然后我又要很麻烦地登录路由 ...
- git push 问题汇总
Q:git push时卡死 这个问题找的快要放弃的时候... A: git config --global http.postBuffer [via] Q:git push 报错 Counting o ...
- Python学习笔记18-发送邮件
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件, ...
- 使用java连接数据库以后显示“ Establishing SSL connection without server's identity verification is not recommended”的警告如何解决
今天写了一段查询数据库的操作,如下 package MySQL; import java.sql.*; public class MySQL { //JDBC驱动名以及数据库URL static fi ...
- URI跳转方式地图导航的代码实践
本文转载至 http://adad184.com/2015/08/11/practice-in-mapview-navigation-with-URI/ 前言 之前介绍了我正在做的是一款定位主打的应用 ...
- codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)
题目链接:http://www.codeforces.com/problemset/problem/580/A题意:求最长连续非降子序列的长度.C++代码: #include <iostream ...