Symfony - 验证空查询参数值

Mal*_*chi 13 php query-parameters symfony fosrestbundle

我正在使用FOSRestBundle,并且想知道是否可以使用注释验证空查询参数?

例如打电话时:/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 ];
}
Run Code Online (Sandbox Code Playgroud)

更新

我也在FOSRestBundle的GitHub仓库中提出了这个问题,由于正在使用的Regex验证器的限制,我看起来目前无法提出要求.

https://github.com/FriendsOfSymfony/FOSRestBundle/issues/814#issuecomment-49696288

Lim*_*ter -3

我对 symfony 不熟悉,但我认为一个简单的

$dealId = isset($dealId) ? $dealId : '';
Run Code Online (Sandbox Code Playgroud)

对你的问题有帮助