Replace hooks by events #8010
Replies: 6 comments 5 replies
-
Thanks @jonasraoni. There's an issue filed to review this for 3.4, so that we can try to get things organized as soon as possible. #7831 I think there's a good case for keeping hooks and events, but using them for separate cases. From the issue:
|
Beta Was this translation helpful? Give feedback.
-
Oh, next time I'll search before writing 😅 Considering that events can fully replace the hooks (they are synchronous by default + the propagation can be stopped), while bringing the PROs that I've wrote, what's the reasoning for keeping both? I think the concept you've mentioned (hooks = before the action, events = after) is enough to cover with a good naming standard |
Beta Was this translation helpful? Give feedback.
-
Another Pro: The event classes are easily identified by an IDE and you can create updated documentation automatically. The documentation for hooks is basically non existing and the best approach to find hooks in the codebase is a string search for HookRegistry::call and sometimes this is not enough because of Hooks placed in parent classes. |
Beta Was this translation helpful? Give feedback.
-
I agree that type hinting is a huge plus, and events are better than hooks in that sense. I have a couple of doubts that I would want addressed before jumping to events.
|
Beta Was this translation helpful? Give feedback.
-
Hi @NateWr , thanks for your detailed writing. Maybe for content display we should keep the HookRegistry and for processing related hooks, e.g. Form processing and Service methods we could think about a naming convention for events like @jonasraoni proposed "BeforeXXX" "AfterXXX" ? |
Beta Was this translation helpful? Give feedback.
-
I've just discovered that Laravel events do not support priority. In other words, when an event has two listeners, there's no way to declare that one should run before the other. Event priority was removed in 5.4 and it's not coming back. In my view, this is a requirement for many hooks. We often want to a hook to be able to say "run me before most other callbacks" or "run me after other callbacks". An example of this would be the |
Beta Was this translation helpful? Give feedback.
-
2022-08-24: This proposal was declined due to the lack of priority management in Laravel events. See this thread.
As we're adopting Laravel, I think it makes sense to replace the hooks by events.
Event::dispatch()/listen()
).Pros:
$args
from the hook).cancel()
) or attributes, instead of manipulating variables passed by reference.Cons:
Beta Was this translation helpful? Give feedback.
All reactions