Вопрос

how to turn off the rendering of html responses in twig?

I'm building a RESTful API and want to completly disable HTML rendering. For example if I request /foo/bar without an oAuth Access Token, symfony2 should responses with a 401 json message instead of a 401 html template.

Greetings

Это было полезно?

Решение

Just don't call the render method : return $this->render('FooBarBundle:Default:index.html.twig');

Instead of this, you can call in your controller :

return new JsonResponse(array(
    'message' => 'Your message here'
), 401);

Другие советы

1- Use fosrestbundle. 2- In your config.yml file, put "defaults: {_format: json}" in configuration bundle. 3- In your controller, just throw new UnauthorizedHttpException('optional custom message'). It should do the job.

If you don't wanna use this bundle: use _format: json in your routing.yml.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top