不自动接线时捆绑控制器中的错误

时间:2019-05-31 22:54:09

标签: symfony

Symfony文档指出public bundles should explicitly configure their services and not rely on autowiring。因此,我正在使用以下内容在我的捆绑软件中配置Controller服务。

<service id="blah\blah\SecurityController">
    <argument type="service" id="security.authentication_utils"/>
    <tag name="controller.service_arguments"/>
    <tag name="container.service_subscriber"/>
</service>

安全性参数是因为我在登录方法中使用了authenticationUtils-> getLastAuthenticationError()。

但是,这样的服务定义会产生弃用错误。 User Deprecated: Auto-injection of the container for "blah\blah\SecurityController" is deprecated since Symfony 4.2. Configure it as a service instead. at /var/www/html/vendor/symfony/framework-bundle/Controller/ControllerResolver.php:64)

如果我仅将autowire="true"添加到上面的服务定义中,该错误就会消失(这时我也不需要该现有参数)。但是,我想遵循Symfony关于显式配置的建议。

当我需要明确包含的autowire设置为true时,我会丢失哪些自动注入的信息?

1 个答案:

答案 0 :(得分:1)

基于弃用情况,我假设您的控制器正在扩展AbstractController。

有两种方法可以解决此问题:

  1. 如果根本不使用或仅勉强使用AbstractController的功能,则根本无法扩展任何功能。如果您使用例如只有AbstractController中的一个函数,您可以选择重新实现它以减少依赖性。 official docs中也提到了这一点。

  2. 如果要继续扩展AbstractController,则需要手动注入容器。例如。像这样:

services.xml

class FooController extends AbstractController {}

FooController.php

Entry
相关问题