Skip to content

Commit 54d6a9e

Browse files
committed
minor symfony#3736 [book] Misc. routing fixes (javiereguiluz)
This PR was merged into the 2.3 branch. Discussion ---------- [book] Misc. routing fixes | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.3+ | Fixed tickets | - Commits ------- 7500435 [book] [routing] minor rewording and fixed some sample code 26f9e3b [book] [routing] fixed a note that wasn't properly updated 355cd5b [book] [routing] reworded the note about generating URLs in console da8b46f [book] [routing] used the American term "bidirectional" instead of the British bi-directional
2 parents f149dcf + 7500435 commit 54d6a9e

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

book/routing.rst

+26-13
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,10 @@ Generating URLs
11341134
---------------
11351135

11361136
The routing system should also be used to generate URLs. In reality, routing
1137-
is a bi-directional system: mapping the URL to a controller+parameters and
1137+
is a bidirectional system: mapping the URL to a controller+parameters and
11381138
a route+parameters back to a URL. The
11391139
:method:`Symfony\\Component\\Routing\\Router::match` and
1140-
:method:`Symfony\\Component\\Routing\\Router::generate` methods form this bi-directional
1140+
:method:`Symfony\\Component\\Routing\\Router::generate` methods form this bidirectional
11411141
system. Take the ``blog_show`` example route from earlier::
11421142

11431143
$params = $this->get('router')->match('/blog/my-blog-post');
@@ -1168,12 +1168,25 @@ route. With this information, any URL can easily be generated::
11681168

11691169
.. note::
11701170

1171-
In controllers that extend Symfony's base
1171+
In controllers that don't extend Symfony's base
11721172
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller`,
1173-
you can use the
1174-
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::generateUrl`
1175-
method, which calls the router service's
1176-
:method:`Symfony\\Component\\Routing\\Router::generate` method.
1173+
you can use the ``router`` service's
1174+
:method:`Symfony\\Component\\Routing\\Router::generate` method::
1175+
1176+
use Symfony\Component\DependencyInjection\ContainerAware;
1177+
1178+
class MainController extends ContainerAware
1179+
{
1180+
public function showAction($slug)
1181+
{
1182+
// ...
1183+
1184+
$url = $this->container->get('router')->generate(
1185+
'blog_show',
1186+
array('slug' => 'my-blog-post')
1187+
);
1188+
}
1189+
}
11771190

11781191
In an upcoming section, you'll learn how to generate URLs from inside templates.
11791192

@@ -1262,19 +1275,19 @@ to ``generateUrl()``:
12621275

12631276
.. note::
12641277

1265-
The host that's used when generating an absolute URL is the host of
1266-
the current ``Request`` object. This is detected automatically. But if
1267-
you generate absolute URLs for scripts run from the command line, this
1268-
won't work. But don't worry! Just see :doc:`/cookbook/console/sending_emails`
1269-
for details.
1278+
The host that's used when generating an absolute URL is automatically
1279+
detected using the current ``Request`` object. When generating absolute
1280+
URLs from outside the web context (for instance in a console command) this
1281+
doesn't work. See :doc:`/cookbook/console/sending_emails` to learn how to
1282+
solve this problem.
12701283

12711284
Summary
12721285
-------
12731286

12741287
Routing is a system for mapping the URL of incoming requests to the controller
12751288
function that should be called to process the request. It both allows you
12761289
to specify beautiful URLs and keeps the functionality of your application
1277-
decoupled from those URLs. Routing is a two-way mechanism, meaning that it
1290+
decoupled from those URLs. Routing is a bidirectional mechanism, meaning that it
12781291
should also be used to generate URLs.
12791292

12801293
Learn more from the Cookbook

0 commit comments

Comments
 (0)