pprgva
pprgva

Reputation: 21

Symfony 5 Reusable Bundles controller has no container set, did you forget to define it as a service subscriber

I have a problem with AbstractController in my Reusable Bundles.

Without it work AbstractController it works.

class MyCustomController  
{
    public function sayHello(){
        return new Response('Hello');
    }
}

With AbstractController it doesn't work:

class MyCustomController extends AbstractController
{
    public function sayHello(){
        return new Response('Hello');
    }
}

I have an error:

reusable_tools.controller.my_custom_controller" has no container set, did you forget to define it as a service subscriber?

The controller is declared as:

<service id="reusable_tools.controller.my_custom_controller" class="Reusable\ToolsBundle\Controller\MyCustomController" public="true"/>
<service id="Reusable\ToolsBundle\Controller\MyCustomController" alias="reusable_tools.controller.my_custom_controller"/>

routes.xml:

<route id="reusable_tools_controller_my_custom_controller" controller="reusable_tools.controller.my_custom_controller:sayHello" path="/" />

debug:container:

Reusable\ToolsBundle\Controller\MyCustomController                                     alias for "reusable_tools.controller.my_custom_controller" 

Any ideas?

Configuration : Symfony 5.2.6 (Full) PHP 8.0.3

Upvotes: 2

Views: 2359

Answers (2)

SoulOfNoob
SoulOfNoob

Reputation: 31

I ran into the same issue today.

To get rid of the first error message:

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

I needed to add the following to the services.yaml inside my bundle:

<namespace>\<Bundle>\Controller\MyCustomController:
  public: true
  calls:
    - method: setContainer
      arguments: ['@service_container']

This makes the service container available to the controller and sets it public - source: https://github.com/Payum/Payum/issues/841

After i added that i got another error message (because i used an autowired service from inside my bundle):

Could not resolve argument $someService of <Controller::method()>, maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?

to fix the autowiring i needed to add the "tags" part from the other answer like this:

<namespace>\<Bundle>\Controller\MyCustomController:
  tags: ['controller.service_arguments']
  calls:
    - method: setContainer
      arguments: ['@service_container']

this addition makes "public" obsolete aparantly - source: https://symfony.com/doc/current/controller/service.html

I hope that helps anyone who stumbles upon this in the future.

Tested on Symfony 5.4 with PHP version 7.4

Upvotes: 3

Robert van Lienden
Robert van Lienden

Reputation: 61

Struggling into the same issue. The issue is that

For now I've decided to add the following lines to my application config/services.yml;

services:
   <namespace>\<Bundle>\Controller\:
        resource: '@Bundle/Controller/'
        tags: ['controller.service_arguments']

Didn't manage to add this to services in my bundle, so for now just adding some instructions to my README.md.

For now this solution works fine.

Upvotes: 0

Related Questions