具有空值的Symfony QueryParam

时间:2017-08-02 08:23:23

标签: symfony controller queryparam

我试图在我的symfony控制器上使用QueryParam。

我不想接受查询中的空参数的问题。但是当我用

定义QueryParam时
@REST\QueryParam(name="test" , description="The test", requirements="\w+", strict=true, nullable=false)

我试图通过mysite.com/test=来保持网络服务的正常运行。

如果param为空,则无需在代码中进行测试即可拒绝请求中的空param

谢谢

1 个答案:

答案 0 :(得分:0)

当测试奇怪时,验证似乎不能仅仅在param_fetcher_listener上使用配置true,但当我使用force时,一切都突然有效:

# config.yml
fos_rest:
    param_fetcher_listener: force

在控制器中:

/**
 * @QueryParam(name="test" , description="The test", requirements="[a-zA-Z]+", strict=true, nullable=false, allowBlank=false)
 */
public function indexAction($test)
{
    // replace this example code with whatever you need
    return new JsonResponse($test);
}

请注意,在我的示例中,有多个验证,只有?test=a才有效。否test?test=?test=1都不会因特定原因而无效。

如果有人知道如何让它与param_fetcher_listener: true一起使用我很好奇:)

另请参阅文档(https://symfony.com/doc/master/bundles/FOSRestBundle/param_fetcher_listener.html)了解使用force

的含义
相关问题