为什么 Symfony 5 控制器必须扩展 AbstractController 才能使自动装配工作?

Why Symfony 5 controllers must extends AbstractController to make autowiring work?

例如,我尝试使用用于自动装配 Logger 服务的构造函数制作一个基本的 TestController。如果我不扩展 AbstractController,我会得到这个错误:

The controller for URI "/" is not callable: Controller "App\Controller\TestController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?

为什么?

Did you forget to tag the service with "controller.service_arguments"?

您是否正确标记了您的自定义控制器?通常,这是扩展 Symfony\Bundle\FrameworkBundle\Controller\AbstractController 的副作用:标签是自动添加的。你可以跳过扩展AbstractController,但是你必须自己添加标签,并且你不能使用一些辅助方法进行路由,安全检查。


有关该问题的更多详细信息:service to resolve the controller relies on the fact that the controller itself can be fetched as a public service from the container. As nowadays, most services are private per default, this needs to be overriden. Another compiler pass 会处理此问题,并且会遍历所有带有标签 controller.service_arguments

的服务