Symfony - Filter Request with FOSRestBundle and body_listener without Annotations?
<p>I am using Symfony2 with the FOSRestBundle. Is it possible to have the functionality of the @QueryParam and @RequestParam annotations without using annotations? </p>

<p>I am trying to build a json api (<a href="http://jsonapi.org/format/" rel="nofollow">format</a>), so I want to allow query params like include, page, filter, fields, and sort. My ideal way to handle this would be:</p>

<ol>
<li>Use the format_listener to detect it is json.</li>
<li>Use a custom <a href="http://symfony.com/doc/master/bundles/FOSRestBundle/body_listener.html" rel="nofollow">body_listener</a> json handler to process the request so that it's similar to <a href="https://github.com/woohoolabs/yin/blob/master/src/JsonApi/Request/Request.php" rel="nofollow">this</a>.</li>
<li>Have the controller validate the query/request params inside the action function, and throw an exception to be handled by the exception controller if it's invalid. (The body_listener would act as a helper to make extracting the data from the request easier in the controller, but the controller makes the decisions of what to do with that data.)</li>
</ol>

<p>I'm mostly stuck on how to make a custom body_listener. I'm not sure if I would need to make a custom decoder or normalizer, and what that class might look like since they don't give any examples.</p>

<p>Rough code of what controller would look like:</p>

<pre><code>&lt;?php
namespace CoreBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;
use FOS\RestBundle\Context\Context;
use Psr\Http\Message\ServerRequestInterface;

class SiteController extends FOSRestController
{   
    public function getAction($id, ServerRequestInterface $request)
    {
        try {
            // Validate $request. This is where the query/request
            // param annotation functionality would be replaced.
        } catch (Exception $e) {
            throw new InvalidRequestException($e);
        }

        $siteService = $this-&gt;get('app.site_service');
        $site = $siteService-&gt;getSite($id);

        $context = new Context();

        $context-&gt;setVersion($request-&gt;getVersion());
        // Ex: /sites/63?fields[sites]=name,address&amp;fields[company]=foo,bar
        if ($request-&gt;hasIncludeFields()) {
            $context-&gt;addAttribute('include_fields', $request-&gt;getIncludeFields()); // Or however to do this
        }

        $view = new View($site, 200);

        $view-&gt;setContext($context);

        return $view;
    }
}
</code></pre>