From ce319c381d16676da628b7f0951d5b90f6b886f9 Mon Sep 17 00:00:00 2001 From: Parraghy Norbert Date: Wed, 9 Nov 2016 15:34:17 +0100 Subject: [PATCH 1/5] Update formats.rst In this case the $request->getRequestFormat() will give back "html" as default value. Possible solution is set _format as parameter in the route to overwrite the format. In my example without _format parameter it will render the article/index.html.twig and with pdf _format it will render the article/index.pdf.twig. --- templating/formats.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templating/formats.rst b/templating/formats.rst index 565255f22bb..2ebaf632f73 100644 --- a/templating/formats.rst +++ b/templating/formats.rst @@ -21,7 +21,9 @@ isn't actually rendered differently based on its format. In many cases, you may want to allow a single controller to render multiple different formats based on the "request format". For that reason, a common pattern is to do the following:: - + /** + * @Route("/{_format}", name="article_show", defaults={"_format": "html"}, requirements={"_format": "html|pdf"} + */ public function indexAction(Request $request) { $format = $request->getRequestFormat(); From 24190e48208fd8ad581e2f0fee4ec3aad28b1f80 Mon Sep 17 00:00:00 2001 From: Parraghy Norbert Date: Wed, 9 Nov 2016 15:46:42 +0100 Subject: [PATCH 2/5] Update formats.rst --- templating/formats.rst | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/templating/formats.rst b/templating/formats.rst index 2ebaf632f73..8c870a93c98 100644 --- a/templating/formats.rst +++ b/templating/formats.rst @@ -21,15 +21,21 @@ isn't actually rendered differently based on its format. In many cases, you may want to allow a single controller to render multiple different formats based on the "request format". For that reason, a common pattern is to do the following:: - /** - * @Route("/{_format}", name="article_show", defaults={"_format": "html"}, requirements={"_format": "html|pdf"} - */ - public function indexAction(Request $request) - { - $format = $request->getRequestFormat(); - return $this->render('article/index.'.$format.'.twig'); - } +.. configuration-block:: + + .. code-block:: php-annotations + + /** + * @Route("/{_format}", name="article_show", defaults={"_format": "html"}, requirements={"_format": "html|pdf"} + */ + public function indexAction(Request $request) + { + $format = $request->getRequestFormat(); + + return $this->render('article/index.'.$format.'.twig'); + } + The ``getRequestFormat`` on the ``Request`` object defaults to ``html``, but can return any other format based on the format requested by the user. @@ -41,7 +47,7 @@ be configured so that ``/contact`` sets the request format to ``html`` while To create links that include the format parameter, include a ``_format`` key in the parameter hash: -.. configuration-block:: +.. configuration-block:: .. code-block:: html+twig From 9b29cb6aa329e4309d3c1a3778602717ee3db13f Mon Sep 17 00:00:00 2001 From: Parraghy Norbert Date: Wed, 9 Nov 2016 15:47:09 +0100 Subject: [PATCH 3/5] Update formats.rst --- templating/formats.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templating/formats.rst b/templating/formats.rst index 8c870a93c98..755d7e8cc81 100644 --- a/templating/formats.rst +++ b/templating/formats.rst @@ -47,7 +47,7 @@ be configured so that ``/contact`` sets the request format to ``html`` while To create links that include the format parameter, include a ``_format`` key in the parameter hash: -.. configuration-block:: +.. configuration-block:: .. code-block:: html+twig From e8d7e467e534cf4ebe6bd0cedb10ba1c830c4d00 Mon Sep 17 00:00:00 2001 From: Parraghy Norbert Date: Fri, 6 Jan 2017 13:21:25 +0100 Subject: [PATCH 4/5] Update formats.rst --- templating/formats.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/templating/formats.rst b/templating/formats.rst index 755d7e8cc81..7475805d323 100644 --- a/templating/formats.rst +++ b/templating/formats.rst @@ -9,10 +9,10 @@ most cases you'll use templates to render HTML content, a template can just as easily generate JavaScript, CSS, XML or any other format you can dream of. For example, the same "resource" is often rendered in several formats. -To render an article index page in XML, simply include the format in the +To render a contact index page in XML, simply include the format in the template name: -* *XML template name*: ``article/index.xml.twig`` +* *XML template name*: ``contact/index.xml.twig`` * *XML template filename*: ``index.xml.twig`` In reality, this is nothing more than a naming convention and the template @@ -27,13 +27,17 @@ pattern is to do the following:: .. code-block:: php-annotations /** - * @Route("/{_format}", name="article_show", defaults={"_format": "html"}, requirements={"_format": "html|pdf"} + * @Route("{_format}", name="contact_list", defaults={"_format": "html"}, requirements={"_format": "html|xml|pdf"}) + * + * @param Request $request + * + * @return Response */ public function indexAction(Request $request) { $format = $request->getRequestFormat(); - return $this->render('article/index.'.$format.'.twig'); + return $this->render('contact/index.'.$format.'.twig'); } @@ -41,7 +45,7 @@ The ``getRequestFormat`` on the ``Request`` object defaults to ``html``, but can return any other format based on the format requested by the user. The request format is most often managed by the routing, where a route can be configured so that ``/contact`` sets the request format to ``html`` while -``/contact.xml`` sets the format to ``xml``. For more information, see the +``/contact/xml`` sets the format to ``xml``. For more information, see the :ref:`Advanced Example in the Routing chapter `. To create links that include the format parameter, include a ``_format`` @@ -51,7 +55,7 @@ key in the parameter hash: .. code-block:: html+twig - + PDF Version @@ -59,9 +63,6 @@ key in the parameter hash: - + PDF Version From 63ecd78bb8e90989eaa6bb69629997afc2bc4271 Mon Sep 17 00:00:00 2001 From: Parraghy Norbert Date: Fri, 6 Jan 2017 13:23:26 +0100 Subject: [PATCH 5/5] Update formats.rst --- templating/formats.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templating/formats.rst b/templating/formats.rst index 7475805d323..eec1d8bd75e 100644 --- a/templating/formats.rst +++ b/templating/formats.rst @@ -27,7 +27,7 @@ pattern is to do the following:: .. code-block:: php-annotations /** - * @Route("{_format}", name="contact_list", defaults={"_format": "html"}, requirements={"_format": "html|xml|pdf"}) + * @Route("/{_format}", name="contact_list", defaults={"_format": "html"}, requirements={"_format": "html|xml|pdf"}) * * @param Request $request *