警告:MyCompany \ CmsBundle \ Controller \ DocumentDuplicateController :: __ construct缺少参数1

时间:2018-07-02 15:15:31

标签: php symfony constructor doctrine

我正在开发基于Symfony 2.7的应用程序。

我想将Doctrine服务注入到我的控制器中,并且想将其作为服务构造函数传递,以避免依赖整个容器。因此,我将以下内容放入我的services.yml文件中:

  app.controller.document_duplicate:
    class: MyCompany\CmsBundle\Controller\DocumentDuplicateController
    arguments:
      - "@doctrine"

...,然后在控制器类中添加以下内容:

public function __construct($doctrine)
{
    $this->doctrine = $doctrine;
}

...行为不符合我的预期。在浏览器中查看控制器时,出现异常:

  

警告:缺少参数1   MyCompany \ CmsBundle \ Controller \ DocumentDuplicateController :: __ construct(),   呼入   /usr/src/app/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php   在第194行并已定义

我需要解决什么才能使控制器看到我要传递给它的服务?

1 个答案:

答案 0 :(得分:0)

原来,我只需要将此注释放在类定义上方,即可使其注意services.yml文件的内容:

/**
 * @Route(service="app.controller.document_duplicate")
 */

做到了。