Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter4 - Event listner
#11

(This post was last modified: 09-29-2018, 11:29 PM by unodepiera.)

(09-29-2018, 12:38 PM)happyape Wrote:
(09-29-2018, 11:47 AM)unodepiera Wrote:
(09-19-2018, 03:21 AM)happyape Wrote: I am just trying to understand if CI4 events (https://bcit-ci.github.io/CodeIgniter4/g...vents.html) are meant to function similar way as of Laravel (https://laravel.com/docs/5.7/events)

I am not comparing two frameworks but instead trying to understand how to achieve it in CI4.

My requirement is to be able to, say, take some actions after a user has registered successfully. I am assuming I'd dispatch an "user_registered_successfully" event and there would be listener registered to take some action.

Fast example:

Config/Events.php
PHP Code:
Events::on('userRegistered', function (User $user) {
 
//send an email to registered user
}); 

AuthController.php
PHP Code:
$userModel = new User;
$userEntity = new \App\Entities\User;
$postEscapedData esc($this->request->getRawInput());
$userEntity->fill($postEscapedData);
$userModel->save($userEntity);
Events::trigger('userRegistered'$userEntity); 

Great! I'll give it a go. Thank you.

Another way directly from CodeIgniter model events:

Models/User.php
PHP Code:
protected $afterInsert = ['sendEmailToUser'];

protected function 
sendEmailToUser(array $data)
{
 
   //TODO send email to new user

Reply


Messages In This Thread
CodeIgniter4 - Event listner - by happyape - 09-19-2018, 03:21 AM
RE: CodeIgniter4 - Event listner - by Pertti - 09-19-2018, 03:58 AM
RE: CodeIgniter4 - Event listner - by kilishan - 09-19-2018, 05:38 AM
RE: CodeIgniter4 - Event listner - by happyape - 09-19-2018, 05:48 AM
RE: CodeIgniter4 - Event listner - by Pertti - 09-19-2018, 06:01 AM
RE: CodeIgniter4 - Event listner - by Pertti - 09-19-2018, 05:54 AM
RE: CodeIgniter4 - Event listner - by happyape - 09-19-2018, 06:03 AM
RE: CodeIgniter4 - Event listner - by Pertti - 09-19-2018, 06:40 AM
RE: CodeIgniter4 - Event listner - by unodepiera - 09-29-2018, 11:47 AM
RE: CodeIgniter4 - Event listner - by happyape - 09-29-2018, 12:38 PM
RE: CodeIgniter4 - Event listner - by unodepiera - 09-29-2018, 11:29 PM
RE: CodeIgniter4 - Event listner - by abrkof - 01-04-2020, 10:31 AM
RE: CodeIgniter4 - Event listner - by InsiteFX - 01-05-2020, 09:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB