From 873706cccac3d6e8808ad13d50aa14c37e2850bc Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Tue, 18 Mar 2014 14:18:31 -0500 Subject: [PATCH 1/2] Moved the section about generating absolute URLs down This is so we have the controller/PHP version and template version in the same spot. See #3573 I also made a few other tweaks, including using generateUrl (like we did earlier) to generate the absolute URL, not going through the router service directly. --- book/routing.rst | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/book/routing.rst b/book/routing.rst index 0a72665bd81..799a1c42bc5 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -1192,29 +1192,6 @@ In an upcoming section, you'll learn how to generate URLs from inside templates. For more information, see the documentation for that bundle. -.. index:: - single: Routing; Absolute URLs - -Generating Absolute URLs -~~~~~~~~~~~~~~~~~~~~~~~~ - -By default, the router will generate relative URLs (e.g. ``/blog``). To generate -an absolute URL, simply pass ``true`` to the third argument of the ``generate()`` -method:: - - $this->get('router')->generate('blog_show', array('slug' => 'my-blog-post'), true); - // http://www.example.com/blog/my-blog-post - -.. note:: - - The host that's used when generating an absolute URL is the host of - the current ``Request`` object. This is detected automatically based - on server information supplied by PHP. When generating absolute URLs for - scripts run from the command line, you'll need to manually set the desired - host on the ``RequestContext`` object:: - - $this->get('router')->getContext()->setHost('www.example.com'); - .. index:: single: Routing; Generating URLs in a template @@ -1227,7 +1204,7 @@ But if you pass extra ones, they will be added to the URI as a query string:: $this->get('router')->generate('blog', array('page' => 2, 'category' => 'Symfony')); // /blog/2?category=Symfony -Generating URLs from a template +Generating URLs from a Template ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The most common place to generate a URL is from within a template when linking @@ -1250,7 +1227,20 @@ a template helper function: Read this blog post. -Absolute URLs can also be generated. +.. index:: + single: Routing; Absolute URLs + +Generating Absolute URLs +~~~~~~~~~~~~~~~~~~~~~~~~ + +By default, the router will generate relative URLs (e.g. ``/blog``). From +a controller, simply pass ``true`` to the third argument of the ``generateUrl()`` +method:: + + $this->getgenerateUrl('blog_show', array('slug' => 'my-blog-post'), true); + // http://www.example.com/blog/my-blog-post + +From a template, it looks like this: .. configuration-block:: @@ -1268,6 +1258,14 @@ Absolute URLs can also be generated. Read this blog post. +.. note:: + + The host that's used when generating an absolute URL is the host of + the current ``Request`` object. This is detected automatically. But if + you generate absolute URLs for scripts run from the command line, this + won't work. But don't worry! Just see :doc:`/cookbook/console/sending_emails` + for details. + Summary ------- From 007a54fa90bca25a1ced3d725b1e68880f6da8c3 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Tue, 18 Mar 2014 21:45:17 -0500 Subject: [PATCH 2/2] Fixing typo thanks to @xabbuh --- book/routing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/routing.rst b/book/routing.rst index 799a1c42bc5..1039f673d63 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -1237,7 +1237,7 @@ By default, the router will generate relative URLs (e.g. ``/blog``). From a controller, simply pass ``true`` to the third argument of the ``generateUrl()`` method:: - $this->getgenerateUrl('blog_show', array('slug' => 'my-blog-post'), true); + $this->generateUrl('blog_show', array('slug' => 'my-blog-post'), true); // http://www.example.com/blog/my-blog-post From a template, it looks like this: