我正在使用 FOSRestBundle,想知道是否可以使用注释来验证空查询参数?
例如,在调用:/comments/1
时,会引发异常,因为 dealId
和 source
查询参数尚未设置。
但是,即使 source
值尚未设置并且与中概述的正则表达式不匹配,调用 /comments/1?dealId=1&source=
也可以。注释。
Controller 功能:
/**
* Get a single comment.
*
* @Annotations\QueryParam(name="dealId", requirements="\d+", strict=true, description="The deal the comments belong to.")
* @Annotations\QueryParam(name="source", requirements="(forum|blog)", strict=true, description="The source of the comments.")
*
* @Annotations\View()
*
* @Annotations\Get("/comments/{id}", requirements={"id" = "\d+"})
*
*/
public function getCommentAction(Request $request, ParamFetcherInterface $paramFetcher, $id)
{
$dealId = $paramFetcher->get('dealId');
$source = $paramFetcher->get('source');
// TODO: Implement
return [ 'id' => $id, 'dealId' => $dealId, 'source' => $source ];
}
更新
我也在 FOSRestBundle 的 GitHub 存储库上提出了这个问题,由于正在使用的正则表达式验证器的限制,看起来我所要求的目前是不可能的。
https://github.com/FriendsOfSymfony/FOSRestBundle/issues/814#issuecomment-49696288
最佳答案
如果您想强制检查参数,您可以按照文档中的说明更改配置文件,示例如下:
fos_rest:param_fetcher_listener:强制
然后您可以相应地设置其他选项,例如 strict、nullable。
在此处查看更多详细信息:
http://symfony.com/doc/current/bundles/FOSRestBundle/configuration-reference.html (archive.org) https://symfony.com/doc/3.x/bundles/FOSRestBundle/index.html#config-reference https://symfony.com/doc/3.x/bundles/FOSRestBundle/annotations-reference.html
关于php - Symfony - 验证空查询参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24635776/