afterLogin Event?

Hey, guys!

I have to perform some operations after the user logs in, but I don’t want to change so much my UserIdentity class to do this

My question is:

Is there any way to create an afterLogin event and attach it to my User model and so raise the event in the Login action?

Something like this?

1 -> User Performs Login

2 -> My controller raises User::afterLogin event

3 -> User::afterLogin event takes care of some changes in database

Any help is appreciated

PS.: I’ve read Yii’s documentation and cookbook, but both are helpless on Events

Regards

:)

Read this cookbook page about events:

http://www.yiiframework.com/doc/cookbook/44/

You should attach an event handler (or some handlers) to the event onAfterLogin. It will look like:




// UserIdentity


public function authenticate()

{

    $this->onAfterLogin = array(new MyClass, 'helpfulMethod');

    $this->onAfterLogin(new CEvent($this));

}


public function onAfterLogin($event)

{

    $this->raiseEvent('onAfterLogin', $event);

}


// MyClass

 

public function helpfulMethod($event)

{

    echo 'Hi, '.Yii::app()->user->id.'!';

}



Hey, Andy!

I’ve had already read the cookbook post you’ve mentioned, however, your explanation is way better, meaning a real world sample, which is exactly what I need.

Very useful and appreciated!

Thanks a lot!!!

Regards!

:)

Hey Junior. Have you had success using this method? I’m trying to solve this for a while and until now nothing…

Useful!!