Unable to find template "Foo:Users:getUsers.html.twig"
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"
},
is this not a duplicate of https://github.com/FriendsOfSymfony/FOSRestBundle/issues/594 ?
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.
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
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.
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.
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
then you need to configure the format listener: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/3-listener-support.md#format-listener
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
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