Symfony: 'controller cannot be fetched from the container because it is private'
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"?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.
Fix it fast
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_arguments2Match what you see, then jump to its fix
- 1
The 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
services.yaml has an explicit entry or resource for App\Controller without tagsThe controllers are registered in services.yaml without the tagFix - 3
_defaults in services.yaml has autoconfigure: false, or is missing, and the controller has no attributeThe default autowire and autoconfigure settings were removedFix
- 1
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.php2Fix
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_argumentsConfirmed by symfony.com
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.yaml2Fix
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_argumentsConfirmed by symfony.com
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.yaml2Fix
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:containerConfirmed by symfony.com
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 fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix