Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 3.4:递归验证器还是可追踪的验证器?_Php_Symfony - Fatal编程技术网

Php Symfony 3.4:递归验证器还是可追踪的验证器?

Php Symfony 3.4:递归验证器还是可追踪的验证器?,php,symfony,Php,Symfony,我的服务有问题 <service id="api.api" class="ApiBundle\Service\ApiService"> <argument type="service" id="request_stack"/> <argument type="service" id="validator"/> </service> 问题是: 环境开发者,验证器必须是TraceableValidator的实例

我的服务有问题

 <service id="api.api" class="ApiBundle\Service\ApiService">
        <argument type="service" id="request_stack"/>
        <argument type="service" id="validator"/>
 </service>
问题是:

  • 环境开发者,验证器必须是TraceableValidator的实例
  • ENV_PROD,验证程序必须是递归验证程序的实例
你知道我为什么会有这种冲突吗

在带有RecursiveValidator的ENV_DEV中,我有以下错误:

Type error: Argument 2 passed to 
ApiBundle\Service\ApiService::__construct() must be an instance of 
Symfony\Component\Validator\Validator\RecursiveValidator, 
instance of Symfony\Component\Validator\Validator\TraceableValidator 
given, called in 
var/cache/dev/ContainerLqjid6c/getApi_ApiService.php on line 8
cache:clear
无法解决问题


感谢您的帮助。

您应该始终(如果可用)提示接口,而不是提示实现。在这种情况下,同时使用和实现

因此,您的构造函数应该如下所示:

public function __construct(RequestStack $requestStack, ValidatorInterface  $validator)
{
    $this->request = $requestStack->getCurrentRequest();
    $this->validator = $validator;
}

您必须使用两个类都实现的接口。如果您使用autowire,服务注入可能会失败,您将不得不执行额外的步骤。如果有任何文档可以解释为什么在ENV_DEV中使用TraceableValidator,而在ENV_PROD中使用RecursiveValidator,我在遇到问题时没有找到任何关于它的文档。这就是工作,谢谢你的建议。我注意到这项建议。再次感谢。
public function __construct(RequestStack $requestStack, ValidatorInterface  $validator)
{
    $this->request = $requestStack->getCurrentRequest();
    $this->validator = $validator;
}