Symfony2 + FOSRestBundle: enable / disable REST function for each controller / action?

Part of my application will be available as an API , so some of my pages should be available in JSON or XML (based on the Accept header of the Type header).

I used FOSRestBundle and it works very well, but now ALL of my pages are available in XML (or JSON) when sending the Accept 'Content Type: Application / XML header.

So, I would like to enable / disable this feature for some of my controllers / actions. I would ideally do this using annotations.

Is it possible?

My config.yml:

fos_rest:
    view:
        formats:
            rss: false
            xml: true 
            json: true
        templating_formats:
            html: true
        force_redirects:
            html: false
        failed_validation: HTTP_BAD_REQUEST
        default_engine: twig
        view_response_listener: force
    body_listener:
        decoders:
            json: acme.decoder.json
            xml: fos_rest.decoder.xml
    format_listener:
        default_priorities: ['html', 'xml', 'json', '*/*']
        fallback_format: html
        prefer_extension: false    
+5
source share
2

RestBundle, XML, View . , @View View::create() , , HTML.

, prefer_extension true :

my_route:
    pattern:  /my-route
    defaults: { _controller: AcmeDemoBundle:action, _format: <format> }

<format> - , .

+6

view_response_listener false ( - force). @View , REST.

.

REST:

/**
 * @Route("/comments")
 */
class CommentsControler extends Controller
{
    /**
     * @Route("/")
     * @Method({"POST"})
     */
    public function newAction() { ... }

    /**
     * @Route("/{id}")
     */
    public function detailAction($id) { ... }

    ...
}

REST. , @View ( ).

/**
 * @View
 * @Route("/api/comments")
 */
class RestfulCommentsControler extends Controller
{
    /**
     * @Route("/")
     * @Method({"POST"})
     */
    public function newAction() { ... }

    /**
     * @Route("/{id}")
     */
    public function detailAction($id) { ... }

    /**
     * @View(statusCode=204)
     * @Route("/{id}/delete")
     */
    public function deleteAction($id) { ... }

    ...
}
  • View FOS\RestBundle\Controller\Annotations\View
  • Route Symfony\Component\Routing\Annotation\Route
+2

All Articles