FOS rest bundle: unable to find template
Asked Answered
P

4

11

I get the exception:

Unable to find template ""

The other similar questions didn't help; and weirdly enough it was working fine and then suddenly started giving me this exception.

composer:

   "friendsofsymfony/rest-bundle": "0.13.*@dev",
   "jms/serializer-bundle": "0.12.*@dev",

I'm following Automatic route generation: single RESTful controller (for simple resources)

config:

fos_rest:
    format_listener: true
    routing_loader:
        default_format: json
    view:
        view_response_listener: 'force'
    serializer:
        serialize_null: true

sensio_framework_extra:
    view:    { annotations: false }
    router:  { annotations: true }

routing:

sectors:
    type: rest
    prefix: /{v}
    resource: JJ\MainBundle\Controller\SectorsController

controller:

/**
 * @RouteResource("Sector")
 */
class SectorsController extends Controller
...
    public function cgetAction()
    {
        return $this->getSectorIndustryRepo()->findAll();
    }
Posh answered 2/8, 2013 at 7:7 Comment(1)
P
11

I managed to resolve this issue by setting the listener explicitly to false:

fos_rest:
    format_listener: false

EDIT

This also works:

fos_rest:
    format_listener:
        prefer_extension: false
        default_priorities:
            - json
Posh answered 3/8, 2013 at 17:28 Comment(2)
I don't think it is the right solution. You just turned off the whole Content Negotiation feature.Murrell
That is not what I told you here: github.com/FriendsOfSymfony/FOSRestBundle/issues/…. But yeah, it is a matter of configuration. Thing is, either you use extension as preferred way to set the format which is bad but... anyway, just turn the prefer_extension parameter to true, or you use the Accept header and you have to configure priorities (Content Negotiation feature).Murrell
M
6

For FOSRestBundle version 2.0 the config should be

fos_rest:
    format_listener:
        rules:
            prefer_extension: false
            fallback_format: json
Manos answered 11/6, 2017 at 18:34 Comment(0)
V
4

(For reference) If One is testing with a rest client or manually be sure to set the header in your request: Accept:application/json

Because fosRestBundle tries to determine the proper response type from the headers in the request.

Vestment answered 23/9, 2015 at 21:28 Comment(1)
This helped me.Sorus
B
2

Annotate your controller action with @View to resolve this issue.

use FOS\RestBundle\View\View;

Have a look at the documentation chapter View Response listener.


If you don't want to annotate every action - you can set the @View annotation for a complete class using my pull request here.

Add this to your composer.json to use the PR.

"require": {
    "friendsofsymfony/rest-bundle": "dev-dev-view-addition as 0.12.0", 

// ...

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/nifr/FOSRestBundle"
    },
Biltong answered 2/8, 2013 at 10:25 Comment(1)
Unable to find template "MainBundle:Sectors:cget.html.twig". I'm already using the extra bundle; config updatedPosh

© 2022 - 2024 — McMap. All rights reserved.