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

12

24

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

Percyperdido answered 6/4, 2020 at 12:28 Comment(5)
Your controller is into a Controller folder ?Fagaceous
That's not enough to tell you what's wrong. If that's all there is in your controller, and your services.yaml has the appropriate defaults for Symfony 5, you should be fine. So something else must be wrong.Backpedal
@GaryHoubre Yes, here's the content of service.yaml. I've tested this for empty bare bones controllers and I get the same error, so I'm assuming the content isn't what's causing the errorPercyperdido
php version? Try to use 7.4.4 if possible. Some of the earlier 7.4.x versions had preloading issues which caused mysterious errors such as these. Might also try deleting var/cache completely.Straightlaced
@Straightlaced Php version is 7.4.4. Deleting var/cache folder solved the problem. Could you please post your answer so I can mark it accepted?Percyperdido
P
21

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.

Percyperdido answered 8/4, 2020 at 11:53 Comment(0)
K
11

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;
Koniology answered 25/10, 2021 at 11:54 Comment(1)
Yes, I also saw the error from only having the namespace Controller;Methylene
M
8

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

Matins answered 14/10, 2020 at 9:26 Comment(4)
The strange thing is I inject it (in xml configuration), but it still hits this line... I also empty the cache with everything new that I tryHornet
In my case, I had to change the service id to the FQN. I do not know if the parameterized classname will be useful now. This is a bit of an old school project.Hornet
Thanks, that fixed it for me! So weird that you have to somehow figure out these quirks.Balloon
The configuration key "calls" is unsupported for the serviceHelyn
A
3

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

Andrej answered 7/7, 2022 at 8:33 Comment(0)
M
3

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

Marcosmarcotte answered 25/11, 2022 at 12:50 Comment(0)
M
2

You have to change:

namespace App\Controller;

with:

namespace App\Controller\Api;
Michelsen answered 13/8, 2022 at 14:3 Comment(0)
C
0

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 ;)

Chickabiddy answered 21/9, 2022 at 14:29 Comment(0)
H
0

(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.

Hippocrene answered 25/12, 2022 at 0:0 Comment(0)
S
0

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

Sokotra answered 12/4, 2023 at 12:31 Comment(0)
A
0
three steps

1. edit ./config/services.yaml

2. edit ./config/routes.yaml

3. edit composer.json

Airwoman answered 8/9, 2023 at 5:27 Comment(2)
meta.https://mcmap.net/q/12237/-windows-cmd-pipe-not-unicode-even-with-u-switchFalls
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewCloudcapped
C
0

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.

Calmative answered 18/9, 2023 at 19:46 Comment(0)
C
-1

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

Chen answered 18/1, 2022 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.