FOSRestBundle icon indicating copy to clipboard operation
FOSRestBundle copied to clipboard

Unable to find template "Foo:Users:getUsers.html.twig"

Open djnivek opened this issue 13 years ago • 9 comments

I get this error:

Unable to find template "RestAPIDemoBundle:Users:getUsers.html.twig"

config.yml:

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

fos_rest:
    format_listener:
        rules:
            - prefer_extension:     false
    view:
        view_response_listener: true

routing.yml:

users:
    type: rest
    resource: RestAPI\DemoBundle\Controller\UsersController

UsersController.php

<?php

namespace RestAPI\DemoBundle\Controller;

use RestAPI\DemoBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

class UsersController extends Controller {

    /**
     * @return array
     * @View()
     */
    public function getUsersAction() {
        $users = $this->getDoctrine()->getRepository('RestAPIDemoBundle:User')->findAll();
        return array('users' => $users);
    }

    /**
     * @param User $user
     * @return array
     * @View()
     * @ParamConverter("user", class="RestAPIDemoBundle:User")
     *
     */
    public function getUserAction(User $user) {
        return array('user' => $user);
    }

}

composer.json

    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.3.*",
        "doctrine/orm": ">=2.2.3,<2.4-dev",
        "doctrine/doctrine-bundle": "1.2.*",
        "twig/extensions": "1.0.*",
        "symfony/assetic-bundle": "2.3.*",
        "symfony/swiftmailer-bundle": "2.3.*",
        "symfony/monolog-bundle": "2.3.*",
        "sensio/distribution-bundle": "2.3.*",
        "sensio/framework-extra-bundle": "2.3.*",
        "sensio/generator-bundle": "2.3.*",
        "incenteev/composer-parameter-handler": "~2.0",
        "jms/serializer-bundle": "@stable",
        "symfony/yaml": "@stable",
        "jms/di-extra-bundle": "@stable",
        "friendsofsymfony/rest-bundle": "@stable",
        "doctrine/doctrine-fixtures-bundle": "dev-master"
    },

djnivek avatar Nov 20 '13 22:11 djnivek

is this not a duplicate of https://github.com/FriendsOfSymfony/FOSRestBundle/issues/594 ?

lsmith77 avatar Nov 20 '13 22:11 lsmith77

Yes because I have the same problem but I can't resolve it.. And for more simplicity I used #594 as template for my post.

djnivek avatar Nov 21 '13 10:11 djnivek

Well, the answer is the same than in #594: it looks like the HTML format is requested. and to render the HTML page, FOSRestBundle expects a template to exist

stof avatar Nov 21 '13 10:11 stof

So I have to add this ?

fos_rest:
    format_listener:
        rules:
            - { fallback_format: html, prefer_extension: false }
    view:
        view_response_listener: true

Sorry, I do not understand very good.

djnivek avatar Nov 21 '13 19:11 djnivek

What is it that you want to do? do you want to generate HTML or json or xml? if you want to generate HTML you need to use a template. if you want to generate json ..then you need to ensure that the request format is json. if you do not use the format listener, then your request needs to end with .json .. otherwise if you use an Accept header you need to configure the format listener.

lsmith77 avatar Nov 21 '13 20:11 lsmith77

I want to generate JSON in the Rest API.

When I write this in the terminal : http http://192.168.0.14/Symfony/web/app_dev.php/users Accept:application/json

djnivek avatar Nov 21 '13 20:11 djnivek

then you need to configure the format listener: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/3-listener-support.md#format-listener

lsmith77 avatar Nov 21 '13 20:11 lsmith77

Thanks @lsmith77 ! It works by modifying config.yml :

fos_rest:
    format_listener:
        rules:
            - { priorities: ['json', 'xml'], fallback_format: json, prefer_extension: false }
    view:
        view_response_listener: true

djnivek avatar Nov 21 '13 20:11 djnivek

Sorry I had to delete my previous as I figured it my possible outputs by this configuration

fos_rest: format_listener: rules: - { priorities: ['rss','html', 'json', 'xml'], fallback_format: json, prefer_extension: false } view: view_response_listener: true

aftabnaveed avatar Apr 25 '15 08:04 aftabnaveed