dougan1205 2018-09-27 13:49
浏览 75
已采纳

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

I'm relatively new to Symfony, and I'm having trouble some trouble. I'm trying to type hint a custom RequestValidator class in the method being called when the endpoint is called.

Using Symfony 3.4

However, I am getting the following error:

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.

Here is my setup:

services.yml file

...
        _defaults:
           autowire: true
           autoconfigure: true

...

routing.yml

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

And then - inside the Index class, I have the following:

<?php

namespace ApiBundle\Endpoints\Healthcheck\v1;

use ApiBundle\Responses\ApiResponse;


class Index extends ApiResponse
{

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

}

When I do debug:autowiring I see my HealthcheckRequest in the list.

Further, when I do the same and try type-hint in the constructor of the Index class, it all works.

And finally, if I try and type hint the Symfony/HttpFoundation/Request, inside the check() method, it instantiates it correctly.

In summary:

Not working :

  • check(HealthcheckRequest $request)

Working:

  • __construct(HealtcheckRequest $request)
  • check(SymfonyRequest $request)

Am I doing something wrong? Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • doushe8577 2019-01-04 13:09
    关注

    It's part of services.yaml already in Symfony 4, but introduced in version 3.3, so this might help:

    # 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']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?