RicardoAgra
RicardoAgra

Reputation: 680

Controller has no container set, did you forget to define it as a service subscriber

I'm trying to refactor some controllers in Symfony 5 server, but suddenly I'm unable to change or create controllers because of the following error:

'App\Controller\{{ControllerName}}' has no container set, did you forget 
to define it as a service subscriber?

This is the controller:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Shiftsummary;
use App\Entity\Station;
use App\Entity\Shift;
use App\Entity\Line;
use \Datetime;

class StartStationController extends AbstractController {
    /**
     * @Route("/start", name="StartStation")
     */

    public function route(Request $request)
    {
      ...
    }  }

This is the content of service.yaml

services:
    _defaults:
        autowire: true  
        autoconfigure: true 

    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

The controllers are inside the src/controller/ folder

enter image description here

Upvotes: 24

Views: 38738

Answers (12)

Xavi Montero
Xavi Montero

Reputation: 10664

In my case, the controller that gave me has no container set, did you forget to define it as a service subscriber was in another project that we included via composer require.

As autowiring/autoloading is controlled by the main application (not required applications, that are treated as libraries), and we are inheriting from an old project progressively being autowired, but not yet all perfect, I had to add the controller as a service in my main services.yaml with zero configuration:

For example, suppose the controller is MySuperController inside a composer-required project named XaviMontero\DirtyBundle, then in the main project I had to add this:

XaviMontero\DirtyBundle\Controller\MySuperController: ~

Merely adding this line makes the controller "appear as an injectable service" in the main app and all the problems disappeared.

Upvotes: 0

Stepan Stepanov
Stepan Stepanov

Reputation: 231

Declaring the controller as autowire: true will also invisibly add the setContainer. It solved the problem in my case.

Upvotes: 0

J.Z.
J.Z.

Reputation: 941

(Another possibility, even more basic than the other suggestions:)

I had a simple controller that was requiring another PHP file, but in the course of some changes that required file couldn't even be compiled. Fixing that required file resolved the somewhat misleading "has no container set..." message.

Upvotes: 0

Dawid Pierzchalski
Dawid Pierzchalski

Reputation: 51

None of above helped in my case. I solved my case by:

calls:
    - [setContainer, ["@service_container"]]

https://symfony.com/doc/5.4/service_container/calls.html

Upvotes: 3

Cedric-J.
Cedric-J.

Reputation: 31

In my case it was an error in the controller name when I used the bin/console command :

bin/console make:controller APIController this command add automatically suffix -Controller to the file.

Then the controller created is called APIControllerController.

Next I made a copy/paste of Auth0 guide to create API nammed APIController.

In the end the file had the name APIControllerController.php and the class APIController: you just had to change the filename and remove the duplicate -Controller suffix.

Or - in general - verify the name of file controller and the class name ;)

Upvotes: 0

Marco Valeri
Marco Valeri

Reputation: 491

You have to change:

namespace App\Controller;

with:

namespace App\Controller\Api;

Upvotes: 2

Martin L&#233;vai
Martin L&#233;vai

Reputation: 179

In my case, the class had just a different name than the file.

Upvotes: 3

mr12086
mr12086

Reputation: 1147

I have just resolved this issue.
Turned out for me - I had miss typed __construct() as a private function, should be public

Upvotes: -1

Alexis Vandepitte
Alexis Vandepitte

Reputation: 2088

I have to add that if you set the wrong namespace, you get this error too:

// src/Controller/Api/ProductController.php

namespace App\Controller;

class ProductController extends AbstractController {}

namespace must follow folders structure.

namespace App\Controller\Api;

Upvotes: 11

RicardoAgra
RicardoAgra

Reputation: 680

The issue had to do with the cached files, deleting /var/cache made the error not occur.

Thanks go to Cerad for point this out.

Upvotes: 21

Sergey Grechin
Sergey Grechin

Reputation: 966

this is just it, your controller did not have container injected. However, framework expects it to be done if your controller inherits from AbstractController.

in ControllerResolver.php: enter image description here

TLDR; What you need is to inject it.

In your services.yaml, add the following setter call

![enter image description here

Upvotes: 9

Related Questions