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

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.
Reply
#2

Had a brief look at both, and I have to say I struggle to see the benefits of Laravel's *examples*, regardless of the framework.

If you define every possible event in your application, wouldn't that create massive overhead / data in memory to check against for every single page request?

I'd personally try to explicitly handle it via models, both in their example where order status is set to dispatched, or in your case, once user has registered, do these X things.

Feels like events are suppose to be used like hooks, giving you access to certain "framework events", not necessarily you app/business logic?

Am I missing something obvious here?
Reply
#3

Events replaced CodeIgniter's Hooks system with something a little more flexible.

Can it be used like Laravel's? Sure.

What are the benefits of using it? That's up to the developer of course. In many cases, you might prefer not to use them but to use something more explicit. In bigger teams, you might have one developer working on a feature and they expose an event whenever X happens. Other team members can tap into that to do things like fire off a notification that inventory dropped to zero, or send a webhook to third parties, or whatever. Library developers can use it as hooks into their own code, etc.

It's there if you want it, but not required to use for anything. However, the amount of memory used by it is pretty minimal just to store the closures that will get triggered.
Reply
#4

No, I am not asking to define every possible event but only to help handle business logic.

I suppose despatching events can be useful to separate code e.g. a learner access their training, completes, and on successful completion receives pdf certficate by email. I can achieve all these through cron jobs by querying the database (select all records which have completion date today and looop through to send certficates out) or I suppose I could have listeners listening to specific events which do their job as dedicated piece of codes. I am just wondering if that's better way than having all my code in one file and using series of if/else.
Reply
#5

Just in case, I was not questioning events concept, just wondered a good use case for it. Glad I figured out it was related to current hooks - CI4 mastered already h aha.

Now that I think about it, it is down to which way you approach things and how modular or connected the teams working on project are.

I almost explicitly work in environment where all developers approach it from point of view that they know what precursor they need to add more actions to, and that action source code is always available for modifications.

Events on other hand are more, future development in mind, hey, this action has happened so I am announcing it to everyone in case someone may be listening down the line, and now they want to add their own actions to it, without touching my code.

Sounds about right?
Reply
#6

(09-19-2018, 05:48 AM)happyape Wrote: I am just wondering if that's better way than having all my code in one file and using series of if/else.

Hmmm, think there are distinction between events and queued jobs.

Events happen within single HTTP request, while queued jobs works with cron-like approach, but instead of having to figure out from specific record data that someone has passed a class, there's just record of that "event" in DB/Redis the you can have dedicated small bit of code generate PDF with, for example, after user has already received their response to their browser.
Reply
#7

(This post was last modified: 09-19-2018, 06:08 AM by happyape.)

I went thorugh CI4's events documentation again and having another thorough look helps me understand it better (that I can 'trigger' custom events and listen to them in config/events.php to call some external methods as required).

Now my attention diverts towards Queues .. Big Grin (I am only trying to learn and improve).
Reply
#8

(09-19-2018, 06:03 AM)happyape Wrote: I am only trying to learn and improve

Same here, been on one product for very long time, sometimes not ideal in terms of learning about new ways to do stuff Big Grin

From personal experience, generating PDFs seems always be quite slow process, so you might be better off looking into queues, and get your web server save "event" in DB or queue, that then is picked up by worker script, potentially on separate worker server that generates and sends the actual file.
Reply
#9

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

(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); 
Reply
#10

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB