How do routes in FOSRestBundle work? How do routes in FOSRestBundle work? symfony symfony

How do routes in FOSRestBundle work?


Please follow the next URL to read the official documentation:http://symfony.com/doc/master/bundles/FOSRestBundle/index.html

To start with this bundle, I would suggest following the single restful controller documentation:http://symfony.com/doc/master/bundles/FOSRestBundle/5-automatic-route-generation_single-restful-controller.html

You will also find clear examples (https://github.com/liip/LiipHelloBundle) about what this bundle can offer.


Few things from the snippets you have posted drew my attention:

The visibility of your controller method is protected whereas it should be public (http://symfony.com/doc/current/book/controller.html)

public function postDatasetAction(Request $request) {     // your code}

The "routing.yml" file created to configure your route shall contain the name of the aforementioned controller method (postDatasetAction instead of DatasetAction):

# routing.ymldata_query:    type: rest    pattern:  /dataset    defaults: {_controller: DataAPIBundle:Dataset:postDatasetAction, _format: json }    requirements:        _method: POST

Please find below an example to setup a route like :

get_items GET ANY ANY /items.{json}

# config.ymlfos_rest:    allowed_methods_listener: true    format_listener:        default_priorities: ['json', html, '*/*']        fallback_format: json        prefer_extension: true    param_fetcher_listener: true    routing_loader:        default_format: json    view:        formats:            json: true        mime_types:            json: ['application/json', 'application/x-json']        force_redirects:            html: true        view_response_listener: force

# routing.ymlcategories:    type:     rest    resource: Acme\DemoBundle\Controller\ItemController

<?phpnamespace Acme\DemoBundle\Controlleruse FOS\RestBundle\Request\ParamFetcher;use FOS\RestBundle\Controller\Annotations as Rest;class ItemController {    /**     * Get items by constraints     *     * @Rest\QueryParam(name="id", array=true, requirements="\d+", default="-1", description="Identifier")     * @Rest\QueryParam(name="active", requirements="\d?", default="1", description="Active items")     * @Rest\QueryParam(name="from", requirements="\d{4}-\d{2}-\d{2}", default="0000-00-00", description="From date")     * @Rest\QueryParam(name="to", requirements="\d{4}-\d{2}-\d{2}", default="0000-00-00", description="End date")     * @Rest\QueryParam(name="labels", array=true, requirements="\d+", default="-1", description="Labels under which items have been classifed")     *     * @Rest\View()     *     * @param  ParamFetcher                                          $paramFetcher     */    public function getItemsAction(ParamFetcher $paramFetcher) {        $parameters = $paramFetcher->all();        // returns array which will be converted to json contents by FOSRestBundle        return $this->getResource($parameters);    }}

P.S. : You will need to add a view to display the resource as an HTML page


you are missing the routing part of FOSRestbundle in the controller:

protected function postDatasetAction(Request $request){  //Query here} // "post_dataset"      [POST] /dataset