Replies: 2 comments · 26 replies
|
The use case makes sense, especially for cross-cutting concerns like transactions, retries, logging, or tracing around the complete controller execution. However, adding a new event around the whole controller dispatch flow might not be the best abstraction. Symfony already has several extension points in the controller lifecycle:
The missing part is a hook that wraps the actual controller invocation after arguments are resolved. A possible alternative would be adding a dedicated event around controller execution, for example: KernelEvents::CONTROLLER_DISPATCHwhere listeners could wrap the execution: public function onControllerDispatch(ControllerDispatchEvent $event): void
{
$event->setResult(
$this->transactionManager->transaction(
fn () => $event->callController()
)
);
}This would avoid re-running argument resolution and would keep the existing lifecycle intact. For transactions specifically, another possible approach is using an attribute on controllers/actions and handling it through an event subscriber, but currently there is no clean event that wraps the complete controller call. The main concern with adding a callback-based event is maintaining the current execution flow and avoiding hidden behavior changes. The event should probably expose the controller execution rather than expose an internal callback. Something like: $response = $event->execute();or: $event->wrap(callable $wrapper);would make the intention clearer. So the feature request seems useful, but the API design should probably focus on providing a stable "around controller execution" extension point rather than exposing the internal dispatch callback. Ref.:
|
|
You can change the controller after its arguments have been resolved by listening to |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello, I wanted to ask about something - I was looking around, seeing if I can easily implement a transaction mechanism on our controllers.
Turns out - it's not as easy, mostly because the required logic is not quite there.
The HttpKernel resolves arguments, dispatches an event, and then calls the controller. As such, it's not really possible to override this natively.
I was wondering, if a feature that'd add a callback with an event dispatching would be accepted.
Basically this:
Instead of the kernel running the requests like so:
It'd be
This would allow subscribers to listen for a "full" dispatch event, making it possible to wrap the callback in extra logic, in our case, a retryable transaction.
This can kinda be achieved already, but it's not a clean way, as you'd absolutely need to dump the first results of resolvers completely, in case they're not yet in a transaction, and override the returned controller (which is fine), but I don't really like the extra resolver call, as it could mess with some logic.
edit: alternate possible implementation without callbacks
All reactions