Symfony 3.4 - 自动服务线在方法中不起作用

Symfony 3.4 - Auto wire of service not working in method

我对 Symfony 比较陌生,遇到了一些麻烦。 我正在尝试在调用端点时在调用的方法中键入自定义 RequestValidator class 提示。

使用 Symfony 3.4

但是,我收到以下错误:

Controller "ApiBundle\Endpoints\Healthcheck\v1\Index::check()" requires that you provide a value for the "$request" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

这是我的设置:

services.yml 文件

...
        _defaults:
           autowire: true
           autoconfigure: true

...

routing.yml

api.Healthcheck:
path: /healthcheck
controller: ApiBundle\Endpoints\Healthcheck\v1\Index::check
defaults: { _format: json }
methods:
    - GET

然后 - 在索引 class 中,我有以下内容:

<?php

namespace ApiBundle\Endpoints\Healthcheck\v1;

use ApiBundle\Responses\ApiResponse;


class Index extends ApiResponse
{

      public function check(HealthcheckRequest $request) {
        var_dump($request);die;
    }

}

当我这样做时 debug:autowiring 我在列表中看到了我的 HealthcheckRequest。

此外,当我做同样的事情并在索引 class 的构造函数中尝试类型提示时,一切正常。

最后,如果我尝试在 check() 方法中键入提示 Symfony/HttpFoundation/Request,它会正确实例化它。

总结:

不工作:

工作:

我是不是做错了什么?感谢任何帮助。

它是 services.yaml 的一部分,已经在 Symfony 4 中,但在 3.3 版中引入,因此这可能会有所帮助:

# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class

ApiBundle\Endpoints\:
    resource: '../../Endpoints/*'
    tags: ['controller.service_arguments']