Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
Description
Description
When we need to execute code before or after controllers, we need to:
- create a listener class and register the event listeners
- in the event listener, check if the incoming request is the master request
- check manually the route name or if the controller::action matches the expected names
- execute the business code
Creating such listener once is ease and works properly. But, the down side of this approach is that we don't have much clarity on what is being executed before or after controllers, mainly business code. (Framework code is ok).
The suggestion is to add two more attributes to the framework where we can declare what hooks will be executed before and/or after controller actions.
WDYT?
Example
The following example will triggerStopwatch::__invoke
service before/after all actions in this controller.
<?php#[Before(Stopwatch::class, ["dev"])]#[After(Stopwatch::class, ["dev"])]class MyController {publicfunctionfooAction():Response {returnnewResponse('foo'); }publicfunctionbarAction():Response {returnnewResponse('foo'); }}
Or in case to add hooks to a specific controller action:
<?phpclass MyController { #[Before(service:'my_stop_watch_service', when: ["dev"])] #[After(service:'my_stop_watch_service', when: ["dev"])]publicfunctionfooAction():Response {returnnewResponse('foo'); }}
This new feature suggestion is to controller actions but could be expanded to console commands as well.
This is not the case to change the request/command workflow. All of this can be done by the native event dispatcher.