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

how to restrict the REST bundle to part of my routes #1004

Closed allan-simon closed 9 years ago

allan-simon commented 9 years ago

I have a backend and my api

the backend is in /backend and the api in .... /api

I'm currently experiencing a strange problem, in my backend when I try to edit a Form containing images I got the following error

An Exception was thrown while handling: Format 'html' not supported, handler must be implemented

If i check the profiler i can see this error:

ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException: "Format 'html' not supported, handler must be implemented" at /vagrant/mvms/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/View/ViewHandler.php line 304 

the controller method is the following

    public function updateSpecialAction(Request $request, Channel $channel)
    {
        $editForm = $this->createEditForm($channel, 'channel_update_special');
        $editForm = $this->addFormSubmitButton($editForm);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $this->doUpdateChannel($channel);

            return $this->redirectToRoute(
                'channel_special_edit',
                array('id' => $channel->getId())
            );
        }
        $this->render(
            'MVMSBackendBundle:Channel:edit.html.twig',
            array(
                'entity'      => $channel,
                'edit_form'   => $editForm->createView(),
            )
        );

    }

and my config.yml for FOSRestBundle is the following

fos_rest:
    disable_csrf_role: ROLE_API
    param_fetcher_listener: true
    view:
        mime_types:
            json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1']
        view_response_listener: 'force'
        formats:
            xml:  true
            json: true
        templating_formats:
            html: false
    format_listener:
        rules:
            - { path: ^/backend, priorities: [html, json, xml ], fallback_format: html, prefer_extension: true }
            - { path: ^/css, priorities: [css, html], fallback_format: css, prefer_extension: true }
            - { path: ^/js, priorities: [js, html], fallback_format: js, prefer_extension: true }
            - { path: ^/fonts, priorities: [woff, ttf, html], fallback_format: html, prefer_extension: true }
            - { path: ^/api, priorities: [ json ], fallback_format: json, prefer_extension: true }
        media_type:
            version_regex: '/(v|version)=(?P<version>[0-9\.]+)/'
    exception:
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    body_listener: true
    routing_loader:
        default_format: json
    body_converter:
        enabled: true

as the exception seems to be thrown by the bundle , i would like to know if it is possible to restrict him to /api so that it does not interfer with the backend ?

allan-simon commented 9 years ago

my bad I forgot the return in the *Action ........