F0ur
(Foureyesxu)
1
Hello All,
开发中遇到些问题,我需要在用户Login的时候做一些事情
首先我继承了CWebUser
class WebUser extends CWebUser
{
}
然后自定义了一个Behavior
class MyBehavior extends CBehavior
{
public function beforeLogin($id, $states, $fromCookie)
{
//TODO
}
}
然后,我先是再WebUser类中这样定义
public $behaviors = array(
'MyBehavior' => array(
'class' => 'application.behaviors.MyBehavior'
)
);
然后发觉不起作用,后来查阅了一些资料,在config/main.php里修改了一些代码
'user'=>array(
'class' => 'WebUser',
'behaviors' => array('application.behaviors.MyBehavior'),
),
依然不起作用
是说WebUser的Behavior和ARBehavior有所不同,还是我的方法不对?
感谢~
F0ur
(Foureyesxu)
2
我查看了源码,发觉CWebUser里beforeLogin不会raiseEvent或者use Behavior
所以把MyBehavior的beforeLogin改为onBeforeLogin,然后在WebUser的beforeLogin里
$this->attachBehavior('MyBehavior', new MyBehavior);
$this->onBeforeLogin($id, $states, $fromCookie);
这样就可以了
不过我觉得可能这里使用CEvent更好一些
参考资料:http://www.yiiframework.com/forum/index.php/topic/8418-afterlogin-event/page__p__42209__hl__afterlogin#entry42209
http://www.yiiframework.com/doc/cookbook/44/