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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

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

When refactoring controllers in Symfony 5, you might suddenly hit a wall where your classes are treated as simple PHP scripts rather than managed services. The error message App\Controller\{{ControllerName}}' has no container set, did you forget to define it as a service subscriber? is the tell-tale sign that the Dependency Injection Container (DIC) doesn't know how to instantiate your class.

This usually happens when migrating from older Symfony versions or when tweaking configuration files like services.yaml. The root cause is rarely a missing file; it is almost always a misconfiguration in the service definition for your controller namespace.

How Symfony Manages Controllers

In modern Symfony applications, controllers are expected to be instances of services, not static classes. When you extend AbstractController, you expect the container to inject dependencies like repositories or event subscribers into the constructor or via setter methods.

If the container cannot find a definition for your controller class, it falls back to loading the class directly using new. However, because these controllers rely on the DIC for their internal wiring, they fail immediately if no container is bound. The error message explicitly suggests defining them as "service subscribers," which is a slight misnomer in this context; you actually need to register the entire namespace or specific classes as services with autowiring enabled.

Configuring services.yaml

The configuration snippet you provided shows the standard setup for Symfony 5:

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']

This configuration looks correct at first glance. The _defaults block ensures that any class found under App\ is automatically registered as a service with autowiring and autoconfiguration enabled. This should theoretically cover your controllers located in src/Controller.

However, the specific error regarding the "container not set" often points to one of two scenarios:

  1. The Controller Namespace Mismatch: Your controller is defined in App\Controller, but your configuration might be targeting a slightly different path or excluding it inadvertently. Ensure that the resource pattern ../src/Controller actually matches the directory where your StartStationController.php resides. If your file is in src/Controller/Admin/, the wildcard might still catch it, but verify the namespace declaration in the PHP file matches the configured prefix exactly.

  2. Explicit Service Definition Interference: Sometimes, developers attempt to manually define a controller service or add tags that conflict with the default registration. If you have previously defined App\Controller\StartStationController explicitly without autowiring flags, it can override the global settings. Check your YAML file for any duplicate definitions under App\Controller\.

Troubleshooting Steps

If you are certain your folder structure and namespace are correct, try the following steps to isolate the issue:

  • Clear the Cache: Symfony caches compiled service definitions. Running php bin/console cache:clear --env=prod (or app.env) can resolve stale configuration issues where the container was built before a recent change to services.yaml.
  • Check for Exclusions: The exclude directive in your App\ definition is critical. Ensure you aren't accidentally excluding the Controller directory if you have added custom folders there.
  • Verify Autoconfigure: The autoconfigure: true flag tells Symfony to scan for attributes and tags defined in your classes (like @Route). If this is set to false, your route annotations will not be detected, though that usually results in a 404 rather than a container error.

Migration Considerations

If you are migrating from Symfony 4 or an older version, be aware that the default configuration behavior changed slightly. In older setups, controllers were often manually registered or relied on specific kernel bootstrapping logic. The modern approach relies heavily on the global _defaults block.

If you find yourself unable to change controllers despite correct configuration, it might indicate that a previous version of the application forced a non-service instantiation pattern. In rare cases involving third-party bundles or custom kernels, you may need to explicitly bind the controller class in config/services.yaml:

App\Controller\StartStationController:
    autowire: true
    autoconfigure: true

However, this is generally unnecessary if the global namespace rule is working. The error persists because the container sees a request to instantiate a class that lacks the necessary service wrapper.

Final Note on Refactoring

When refactoring, always verify that the new directory structure aligns with your services.yaml patterns. If you move controllers from src/Controller to src/App/Controller, remember to update the resource path or the namespace prefix in your YAML file. Symfony is flexible, but it requires explicit guidance on which classes should be treated as services.

For those looking for more examples of service configuration and best practices in the PHP ecosystem, resources like https://laravelcompany.com offer insights into similar dependency injection patterns across different frameworks.

Our network