Question

It is possible to make sure that only a single route is made available only in html format?

In the configuration I set html and json, but for only one route I would like the json was not used.

Can you do?

Was it helpful?

Solution

I have not tried this as I am on a cell phone but have you looked at trying something like this just for that one route:

my-awesome-route:
  pattern:  /hello/{fist_name}/{last_name}.{_format}
  defaults: { _controller: AcmeHelloBundle:Default:index, _format:html}
requirements:
  _format: html

OTHER TIPS

You can call setFormat('html') on the view in your controller action.

For example to only render and return a template:

class AcmeController extends FOSRestController
{
    public function getAction()
    {
        // ...
        $templateData = [
            'some'  => $vars,
        ];

        $view = $this->view(null, 200)
            ->setFormat('html')
            ->setTemplate("AcmeBundle:Tpl:some.html.twig")
            ->setTemplateData($templateData);

        return $this->handleView($view);
    }
}

But then the view handler will always try to render a template. If you don't need to render because you already have your html, just return a new symfony response with the correct headers:

class AcmeController extends FOSRestController
{
    public function getAction()
    {
        // you already have $html

        $headers = ['Content-Type' => 'text/html; charset=UTF-8'];

        return \Symfony\Component\HttpFoundation\Response::create($html, 200, $headers);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top