Skip to content

[FrameworkBundle] Controller resolver doesn't inject container for invokable controller (no specified method in route) #31271

Closed
@andrew-demb

Description

@andrew-demb

Symfony version(s) affected: 3.4.26

Description

Got fatal Call to a member function has() on null due to not injected container (service locator) in controller, extended from Symfony\Bundle\FrameworkBundle\Controller\AbstractController.
Actual when in route I use DI service id for invokable controller.

How to reproduce

Create controller

<?php

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class IndexController extends AbstractController
{
    public function __invoke()
    {
        $hasRouter = $this->has('router');

        $response = $hasRouter ? 'router exists' : 'router not found';

        return new Response($response);
    }
}

Register in services:

services:
    app.index_controller:
        class: IndexController
        public: true

Use in route:

index:
    path: /
    controller: app.index_controller

Go to / path.

Possible Solution
Change if statement to work with route without :-notation, and not only with array-callables.

if (1 === substr_count($controller, ':') && \is_array($resolvedController)) {

Workaround for me:

index:
    path: /
    controller: app.index_controller:__invoke

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions