Php Symfony-验证空查询参数值
我正在使用FOSRestBundle,想知道是否可以使用注释对空查询参数进行验证 例如,在调用:Php Symfony-验证空查询参数值,php,symfony,fosrestbundle,query-parameters,Php,Symfony,Fosrestbundle,Query Parameters,我正在使用FOSRestBundle,想知道是否可以使用注释对空查询参数进行验证 例如,在调用:/comments/1时,由于未设置dealId和source查询参数,因此会引发异常 但是,调用/comments/1?dealId=1&source=是可以的,即使source值尚未设置,并且与注释中列出的正则表达式不匹配 控制器功能: /** * Get a single comment. * * @Annotations\QueryParam(name="dealId", require
/comments/1
时,由于未设置dealId
和source
查询参数,因此会引发异常
但是,调用/comments/1?dealId=1&source=
是可以的,即使source
值尚未设置,并且与注释中列出的正则表达式不匹配
控制器功能:
/**
* 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 ];
}
/**
* 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, allowBlank=false, 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');
}
更新
我在FOSRestBundle的GitHub回购协议中也提出了这个问题,由于所使用的正则表达式验证器的局限性,我所要求的似乎目前不可能实现
我不熟悉symfony,但我认为
$dealId = isset($dealId) ? $dealId : '';
将有助于解决问题
strict
参数时设置可为空的
参数(true或false)。试着设置它
我想你想要:
@Annotations\QueryParam(name="dealId", requirements="\d+", strict=true, nullable=false, description="The deal the comments belong to.")
@Annotations\QueryParam(name="source", requirements="(forum|blog)", strict=true, nullable=false, description="The source of the comments.")
还可以在中阅读更多关于QueryParam的信息。棘手的部分是允许
源代码和交易ID
为空,但我认为通过
将这些参数添加到路由中(因此必须指定这些参数才能访问控制器),并为每个参数使用字符串前缀(即dealid\u
和source\u
),因此可以指定空值
您还需要修改regex要求以允许空值
/**
* Get a single comment.
*
* @Annotations\View()
* @Annotations\Get("/comments/{id}/dealid_{dealId}/source_{source}",
* requirements={"id" = "\d+", "dealId" = "\d*", "source" = "(forum|blog)*"})
*/
public function getCommentAction(Request $request,
ParamFetcherInterface $paramFetcher, $id, $dealId, $source)
{
return [ 'id' => $id, 'dealId' => $dealId, 'source' => $source ];
}
只需使用QueryParam的allowBlank
选项。在您的情况下,您可以将allowBlank设置为false
,以获得预期的行为:
allowBlank选项还不在FOSRestBundle中,但我为FOSRestBundle提供了一个补丁,它很有可能在下一个版本,即捆绑包的1.5.0版中登陆
这就是控制器的外观:
/**
* 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 ];
}
/**
* 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, allowBlank=false, 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');
}
如果要强制检查参数,可以按照文档中的说明更改配置文件,示例如下:
fos\u rest:param\u fetcher\u listener:force
然后您可以相应地设置其他选项,如strict、nullable
请参阅此处的更多详细信息:
(archive.org)
两者都不能为空,必须同时提供。@Malachi如果我理解正确,必须指定它们,但它们可能被指定为空字符串。设置null
参数并不能解决问题,例如。&source=
在失败时通过:(第一件事:你仍然有异常吗?没有抛出异常。它只是在输出返回的结果['id'=>$id,'dealId'=>$dealId,'source'=>$source]时,就好像它是有效的一样。)
调用/comments/1
时,我的意思是。这对您来说是个问题吗?您没有指定。调用/comments/1
时,会抛出一个异常,说明必须提供这两个参数。但是,如果使用例如/comments/1?dealId=1&source=
或/comments/1?dealId=&source=forum
进行调用,它仍然存在ll的处理方式就好像它是有效的一样。将nullable设置为force并不会改变我认为应该改变的任何事情:(我真的希望在注释中完成它,并且它必须是框架允许的。不幸的是,即使没有框架,它也不会有帮助。当GET params被转换为全局变量时,这是一个解决方案(可以在php.ini中配置),但在任何其他情况下都不能配置。并且像isset($\u GET['param')这样的测试
使用Symfony也不是一个好的做法。Symfony有Request
对象来处理请求参数,但OP希望使用FOS捆绑注释来管理它。如果您在上面宣布变量,它肯定是isset()
除非为NULL。这不仅与主题无关,而且是一种不好的做法,甚至不适用于PHP>5.2。如下面的注释所述,在注释中使用allowBlank=false。