FOSRestBundle
FOSRestBundle copied to clipboard
Added support for query string parameter converter.
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'
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'
@xabbuh @GuilhemN
@lopesrichard Are you sure? I have a different opinion. Grouped parameters are much more user-friendly in the code.
I think that this implementation is too much opinionated. Pagination has always been a tricky topic.
-
pagination[limit]=1&pagination[offset]=2
-
limit=1&offset=2'
-
page=1&size=100'
-
page=1'
- The
Range
header - whatever...
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.