简体   繁体   中英

Symfony2 - Requirements for post parameters

I am relativeley new to Symfony and building a Rest API with the FriendsOfSymfony RestBundle.

My problem is that I want to use the requirements, which I used for my GET routes, for my POST routes. 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.

I'd be greatful for any help on this topic.

The ParamFetcher works just fine with POST parameters as well. You can use the annotation:

use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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