简体   繁体   English

Symfony 3.4:RecursiveValidator还是TraceableValidator?

[英]Symfony 3.4 : RecursiveValidator or TraceableValidator?

I have a problem with my service. 我的服务有问题。

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

The __construct is : __construct是:

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

The problem is: 问题是:

  • ENV_DEV, the validator must be an instance of TraceableValidator ENV_DEV,验证器必须是TraceableValidator的一个实例
  • ENV_PROD, the validator must be an instance of RecursiveValidator ENV_PROD,验证器必须是RecursiveValidator的一个实例

Do you know why I have this conflict ? 你知道我为什么会有这场冲突吗?

In ENV_DEV with RecursiveValidator, I have this error : 在带有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 doesn't solve the problem. cache:clear不解决问题。

Thank you for your help. 谢谢您的帮助。

Instead of hinting to an implementation, you should always (if available) hint to an interface. 您应始终(如果可用)提示接口,而不是暗示实现。 In this case, both RecursiveValidator and TraceableValidator implement the ValidatorInterface . 在这种情况下, RecursiveValidatorTraceableValidator实现了ValidatorInterface

So your constructor should look like this: 所以你的构造函数应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM