FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 703 forks source link

Added support for query string parameter converter. #2230

Closed nuvolapl closed 1 year ago

nuvolapl commented 4 years ago

Usage

config/packages/fos_rest.yaml:

fos_rest:
    query_string_converter:
        enabled: true
# (...)

src/Controller/ExampleController:

<?php

declare(strict_types=1);

namespace App\Controller;

use FOS\RestBundle\Pagination\LimitOffsetPagination;
use FOS\RestBundle\Filter\IdFilter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

class ExampleController
{
    /**
     * @ParamConverter("pagination", converter="fos_rest.query_string")
     * @ParamConverter("filter", converter="fos_rest.query_string")
     */
    public function cget(LimitOffsetPagination $pagination, IdFilter $filter): iterable
    {
        // (...)
    }
}

Example request

curl 'http://localhost/api/examples?pagination[limit]=1&pagination[offset]=2&filter[id]=526e8fc9-9ba2-493b-ab96-4213309ec82f'
lopesrichard commented 4 years ago

I've been searching for the same solution. But sounds better using the query params without the array sintax.

Ex: curl 'http://localhost/api/examples?limit=1&offset=2'

nuvolapl commented 4 years ago

@xabbuh @GuilhemN image

@lopesrichard Are you sure? I have a different opinion. Grouped parameters are much more user-friendly in the code.

goetas commented 2 years ago

I think that this implementation is too much opinionated. Pagination has always been a tricky topic.

Unless there is a general agreement on what should be done, I would prefer to now add this into the core of this bundle

maybe only the query string conversion could be extracted.