Skip to content

Tweak to Absolute URL generation #3693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 19, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions book/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -1250,7 +1227,20 @@ a template helper function:
Read this blog post.
</a>

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->generateUrl('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::

Expand All @@ -1268,6 +1258,14 @@ Absolute URLs can also be generated.
Read this blog post.
</a>

.. 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
-------

Expand Down