`
dcj3sjt126com
  • 浏览: 1827364 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

YII学习第十二天,当前用户相关

    博客分类:
  • YII
yii 
阅读更多

常用的用法是:

class Controller extends CController
{
    public $user = null;
    $this->user = Yii:app()->user;
}

 

this->user->isGuest; 
this->user->id; 
this->user->name; 

还有设置session 
this->user->setStatus('xx'); 
this->user->getStatus('xx'); 

查看一下手册后,发现user的属性和方法还真多。 

详细如下: 
CWebUser represents the persistent state for a Web application user. 

CWebUser is used as an application component whose ID is 'user'. Therefore, at any place one can access the user state via 

Yii::app()->user. 

CWebUser should be used together with an identity which implements the actual authentication algorithm. 

A typical authentication process using CWebUser is as follows: 
1.The user provides information needed for authentication. 
2.An identity instance is created with the user-provided information. 
3.Call IUserIdentity::authenticate to check if the identity is valid. 
4.If valid, call CWebUser::login to login the user, and Redirect the user browser to returnUrl. 
5.If not valid, retrieve the error code or message from the identity instance and display it. 


The property id and name are both identifiers for the user. The former is mainly used internally (e.g. primary key), while the 

latter is for display purpose (e.g. username). The id property is a unique identifier for a user that is persistent during the 

whole user session. It can be a username, or something else, depending on the implementation of the identity class. 

Both id and name are persistent during the user session. Besides, an identity may have additional persistent data which can be 

accessed by calling getState. Note, when cookie-based authentication is enabled, all these persistent data will be stored in 

cookie. Therefore, do not store password or other sensitive data in the persistent storage. Instead, you should store them 

directly in session on the server side if needed. 


1,属性 
allowAutoLogin      boolean        whether to enable cookie-based login. CWebUser 
authTimeout         integer        timeout in seconds after which user is logged out if inactive. CWebUser 
autoRenewCookie     boolean        whether to automatically renew the identity cookie each time a page is requested. CWebUser 
autoUpdateFlash     boolean        whether to automatically update the validity of flash messages. CWebUser 
behaviors           array          the behaviors that should be attached to this component. CApplicationComponent 
flashes             array          Returns all flash messages. CWebUser 
guestName           string         the name for a guest user. CWebUser 
id                  mixed          the unique identifier for the user. CWebUser 
identityCookie      array          the property values (in name-value pairs) used to initialize the identity cookie. CWebUser 
isGuest             boolean        whether the current application user is a guest. CWebUser 
isInitialized       boolean        Checks if this application component bas been initialized. CApplicationComponent 
loginUrl            string|array   the URL for login. CWebUser 
name                string         Returns the unique identifier for the user (e.g. username). CWebUser 
returnUrl           string         Returns the URL that the user should be redirected to after successful login. CWebUser 
stateKeyPrefix      string         a prefix for the name of the session variables storing user session data. CWebUser 



2,方法 
最基本的方法 
除了call, get, isset, set, unset方法之外,还有 
getIsInitialized() Checks if this application component bas been initialized. //一般不需要检查 
checkAccess() Performs access check for this user.   //检查用户可以访问的操作 
方法原型:public boolean checkAccess(string $operation, array $params=array ( ), boolean $allowCaching=true) 


基本方法 
getId() Returns the unique identifier for the user. If null, it means the user is a guest. 
setId() Sets the unique identifier for the user. If null, it means the user is a guest. 
getName() Returns the unique identifier for the user (e.g. username). 
setName() Sets the unique identifier for the user (e.g. username). 
setReturnUrl() Sets the URL that the user should be redirected to after login. 
getReturnUrl() Returns the URL that the user should be redirected to after successful login. 
canGetProperty() Determines whether a property can be read. 
canSetProperty() Determines whether a property can be set. 


登陆相关 
login() Logs in a user. CWebUser 
loginRequired() Redirects the user browser to the login page.//该方法非常好用 
logout() 
getIsGuest() 


增加行为相关 
attachBehavior() Attaches a behavior to this component. CComponent 
attachBehaviors() Attaches a list of behaviors to the component. 
detachBehavior() Detaches a behavior from the component. CComponent 
detachBehaviors() Detaches all behaviors from the component. CComponent 
disableBehavior() Disables an attached behavior. CComponent 
disableBehaviors() Disables all behaviors attached to this component. CComponent 
enableBehavior() Enables an attached behavior. CComponent 
enableBehaviors() Enables all behaviors attached to this component. 


session相关 
setState() Stores a variable in user session. // 是基于cookie-based authentication,所以不应存一些如密码等敏感信息 
getState() Returns the value of a variable that is stored in user session. 
hasState() Returns a value indicating whether there is a state of the specified name. 
clearStates() Clears all user identity information from persistent storage. 
setStateKeyPrefix() Sets a prefix for the name of the session variables storing user session data. 
getStateKeyPrefix() Returns a prefix for the name of the session variables storing user session data. 


flash相关 
hasFlash() Determines whether the specified flash message exists 
getFlash() Returns a flash message. 
setFlash() Stores a flash message. 
getFlashes() Returns all flash messages. 


事件相关 
raiseEvent() Raises an event. 
hasEvent() Determines whether an event is defined. 
hasEventHandler() Checks whether the named event has attached handlers. 
getEventHandlers() Returns the list of attached event handlers for an event. 
attachEventHandler() Attaches an event handler to an event. 
detachEventHandler() Detaches an existing event handler. 

分享到:
评论

相关推荐

    YII 学习笔记

    这是开源PHP开发框架Yii全方位教程,想要学习yii框架吗,这个笔记可以快速让你了解yii,撑握yii

    Yii2.0中文学习手册

    Yii2.0中文学习手册

    Yii2—yii2学习—某课网学习笔记

    yii2学习笔记,一边看某课网一遍实践一遍总结,整理文档

    Yii框架学习笔记

    Yii提供了今日Web 2.0应用开发所需要的几乎一切功能。Yii是最有效率的PHP框架之一。Yii是创始人薛强的心血结晶,于2008年1月1日开始开发。

    Yii2.0学习笔记完全版

    Yii2.0学习笔记完全版 Yii2 Yii2.0 Yii2.0学习 Yii2.0学习笔记

    yii学习!!

    yii学习、PHP学习框架学习

    Yii框架学习手册

    Yii Yii框架 Yii框架快速入门,自己整理的很全的,很容易看懂

    yii框架yii框架yii框架yii框架yii框架

    很好用yii框架很好用yii框架很好用yii框架很好用yii框架很好用yii框架很好用yii框架很好用yii框架很好用yii框架很好用yii框架很好用yii框架很好用yii框架很好用yii框架很好用yii框架

    Yii框架学习笔记.pdf

    yii框架学习笔记,主要详解请求的处理流程,yii组件、事件和行为,国际化,日志,视图,以及控制台应用。 带注释的源代码下载:https://github.com/kaimixu/yii-book

    yii2数据缓存 学习笔记

    yii 学习笔记,其中包括数据缓存、缓存组件、缓存过期、缓存依赖的理解及yii2数据缓存的测试代码

    深入理解Yii2.0

    《深入理解Yii2.0》是一本干货。主要讲解Yii2.0及所代表的最新一代Web开发框架的新特性、新技术、新理念、新模式。 采用的方式是分析框架的源代码,尝试从根上进行理解和阐述,并融入个人使用Yii开发的一些经验和...

    Yii2.0打造京东商城平台PHP电商实战Yii2.0视频教程

    教程名称:Yii2.0 打造京东商城...所以想要系统学习Yii2.0这个框架这套教程可以从实战项目带你入手。不错的学习YII框架的基础课程,电商系统的结构完整、框架资源太大,传百度网盘了,链接在附件中,有需要的同学自取。

    yii2-semantic-ui, Yii2的语义用户界面扩展.zip

    yii2-semantic-ui, Yii2的语义用户界面扩展 Yii2语义用户界面扩展语义用户界面( 用于 Yii2的扩展)安装yii2-semantic-ui 2.* 使用语义用户界面 2.*作曲家安装这里扩展的首选方法是通过 Composer 。运行php composer.

    yii框架中文手册教程和YII模板

    yii框架中文手册教程和YII模板 我很辛苦才找到的

    yii2初学者

    yii2是php框架,非常易学。该书是yii2的入门资料之一。建议阅读完自带文档后,看本书。

    yii framework(Yii)框架window版

    Yii采用严格的OOP编写,并有着完善的库引用以及全面的教程。从 MVC,DAO/ActiveRecord,widgets,caching,等级式RBAC,Web服务,到主题化,I18N和L10N,Yii提供了今日Web 2.0应用开发所需要的几乎一切功能。事实上...

    Yii 框架简介,主流PHP框架,值得学习。

    Yii 框架简介 全面轻松入手Yii 从这里开始 简单 明了 言简意赅

    yii1.1.10 开发包(包含yii权威指南以及yii博客例子讲解)

    yii1.1.10 开发包(包含yii权威指南以及yii博客例子讲解)

    Yii 中文帮助文档

    Yii 是什么 Yii 是一个基于组件、用于开发大型 Web 应用的高性能 PHP 框架。它将 Web 编程中的可重用性发挥到极致,能够显著加速开发进程。Yii(读作“易”)代表简单(easy)、高效(efficient)、可扩展(extensible)。...

    Yii权威指南思维导图

    Yii权威指南思维导图 yii类结构 类导图 权威指南

Global site tag (gtag.js) - Google Analytics