Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Symfony: 'controller cannot be fetched from the container because it is private'

ErrorController "App\Controller\HelloController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?

Short answer

Symfony fetches controllers from the container by id, and private services cannot be fetched, so the controller must be marked as a controller. Extend AbstractController, add #[AsController] or #[Route] to the class, or tag the service controller.service_arguments. The tag also makes it public.

  • Applies to Symfony 7.3, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at how the container knows the class, and at the tags it carries. A controller without the controller.service_arguments tag is the usual gap.

    php bin/console debug:container 'App\Controller\HelloController'
    php bin/console debug:container --tag=controller.service_arguments
  2. 2Match what you see, then jump to its fix

    1. 1The class does not extend AbstractController and has no #[AsController] or class-level #[Route]; debug:container does not list it under the tagThe class is a plain class that nothing marks as a controllerFix
    2. 2services.yaml has an explicit entry or resource for App\Controller without tagsThe controllers are registered in services.yaml without the tagFix
    3. 3_defaults in services.yaml has autoconfigure: false, or is missing, and the controller has no attributeThe default autowire and autoconfigure settings were removedFix

What does the error look like?

  • Controller "App\Controller\HelloController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?

Cause 1 of 3

The class is a plain class that nothing marks as a controller

1Check

Symfony tags a class automatically when it extends AbstractController, uses #[AsController] or #[Route], or when the tag is set by hand. Without one of those the resolver cannot fetch it.

grep -n "class \|#\[AsController\|#\[Route" src/Controller/HelloController.php

2Fix

Mark it with the attribute. Putting #[Route] on the class is enough too.

use Symfony\Component\HttpKernel\Attribute\AsController;

#[AsController]
class HelloController
{
    // ...
}

3Verify

The class now appears under the tag, and the route works.

php bin/console debug:container --tag=controller.service_arguments

Confirmed by symfony.com

Did this fix it?

Cause 2 of 3

The controllers are registered in services.yaml without the tag

1Check

A manual service definition for a controller needs the tag, because the tag is what sets it public and non-lazy. The documented form imports the whole folder with the tag.

grep -n -B1 -A3 "App\\\\Controller" config/services.yaml

2Fix

Add the tag to the controller resource.

# config/services.yaml
App\Controller\:
    resource: '../src/Controller/'
    tags: ['controller.service_arguments']

3Verify

Clear the cache and request the route again.

php bin/console debug:container --tag=controller.service_arguments

Confirmed by symfony.com

Did this fix it?

Cause 3 of 3

The default autowire and autoconfigure settings were removed

1Check

Autoconfigure is what detects the purpose of a service from its base class or attributes and tags it. The Symfony page calls a setup without autowiring and autoconfiguration uncommon and not recommended.

sed -n 1,20p config/services.yaml

2Fix

Restore the default block, which also keeps AbstractController working.

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\:
        resource: '../src/'

3Verify

Run the container check, then request the route.

php bin/console lint:container

Confirmed by symfony.com

Did this fix it?

How do I stop it coming back?

  • Keep the default _defaults block from the Symfony skeleton.
  • Put #[AsController] or #[Route] on every controller class, even a plain one.
  • Run php bin/console lint:container in CI before deploying.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Symfony returns 404 'object not found by EntityValueResolver'

Cannot find mapping for "App\Entity\Products": declare one using either the #[MapEntity] attribute or mapped route parameters.

The EntityValueResolver looked for the entity from your route and got null, and the controller argument is not nullable, so Symfony throws a 404.

4 causes, each with a fix

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.