Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Closed
@andrew-demb

Description

@andrew-demb
Issue body actions

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.