简体   繁体   English

Symfony2-后期参数要求

[英]Symfony2 - Requirements for post parameters

I am relativeley new to Symfony and building a Rest API with the FriendsOfSymfony RestBundle. 我是Symfony的新手,并使用FriendsOfSymfony RestBundle构建Rest API。

My problem is that I want to use the requirements, which I used for my GET routes, for my POST routes. 我的问题是我想将用于GET路由的要求用于POST路由。 Is there any way to do so? 有什么办法吗?

So practically something like this: 所以实际上是这样的:

@Post("/article/{articleId}", requirements={"articleId"="\d+"})

Apparantly the ParamFetcher is also only for GET parameters although I couldn't find any specific information about this. 显然,ParamFetcher也仅适用于GET参数,尽管我找不到与此有关的任何特定信息。

I'd be greatful for any help on this topic. 对于这个主题的帮助,我将不胜感激。

The ParamFetcher works just fine with POST parameters as well. ParamFetcher也可以与POST参数配合使用。 You can use the annotation: 您可以使用注释:

use FOS\\RestBundle\\Controller\\Annotations\\RequestParam; 使用FOS \\ RestBundle \\ Controller \\ Annotations \\ RequestParam;

/**
 * @RequestParam(
 *   name="",
 *   key=null,
 *   requirements="",
 *   default=null,
 *   description="",
 *   strict=true,
 *   array=false,
 *   nullable=false
 * )
 */

Just remember to enable the related listeners otherwise it won't work: 请记住要启用相关的侦听器,否则它将不起作用:

fos_rest:
    param_fetcher_listener: true
    body_listener: true

Reference and documentation here: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/3-listener-support.md 参考和文档在这里: https : //github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/3-listener-support.md

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

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