In Symfony kernel.controller event, what is the return type of ControllerEvent::getController()?

Viewed 306

In Symfony 5.3, I have an EventSubscriber to the KernelEvents::CONTROLLER event.

class PrivateAPIEventSubscriber implements EventSubscriberInterface
{
    
    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::CONTROLLER => ['onKernelController', 2],
        ];
    }
    
    public function onKernelController(ControllerEvent $event): void
    {

        $controller = $event->getController();

        dd($controller);

The issue I have is that for most of my requests, the getController() method returns array like this:

PrivateAPIEventSubscriber.php on line 42:
array:2 [▼
  0 => App\Controller\CP\SecurityController {#241 ▶}
  1 => "login"
]

the 0th element is the $controller object and the 1st element is the function.

But for an error page (e.g. no route)

PrivateAPIEventSubscriber.php on line 42:
Symfony\Component\HttpKernel\Controller\ErrorController {#468 ▼
  -kernel: Symfony\Component\HttpKernel\HttpKernel {#141 ▶}
  -controller: "error_controller"
  -errorRenderer: Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer {#469 ▶}
}

I cannot find documentation for this method, the source code is not helpful (the $controller variable inside ConrtrollerEvent is not typed) and I am worried that there might be some more cases which I am not handling (like return of larger arrays or multi-dimensional arrays)

So what are all the expected return types?

1 Answers

The source is your friend:

public function getController(): callable
{
        return $this->controller;
}

It's type-hinted, so it's clear that it returns a callable. Something you can call. It does not return "different return types", but a single return type that can have multiple shapes, as long as it's a valid callable.

To see what the data type details, you can always check the docs.

Related