@@ -1134,10 +1134,10 @@ Generating URLs
1134
1134
---------------
1135
1135
1136
1136
The routing system should also be used to generate URLs. In reality, routing
1137
- is abi-directional system: mapping the URL to a controller+parameters and
1137
+ is abidirectional system: mapping the URL to a controller+parameters and
1138
1138
a route+parameters back to a URL. The
1139
1139
:method: `Symfony\\ Component\\ Routing\\ Router::match ` and
1140
- :method: `Symfony\\ Component\\ Routing\\ Router::generate ` methods form thisbi-directional
1140
+ :method: `Symfony\\ Component\\ Routing\\ Router::generate ` methods form thisbidirectional
1141
1141
system. Take the ``blog_show `` example route from earlier::
1142
1142
1143
1143
$params = $this->get('router')->match('/blog/my-blog-post');
@@ -1168,12 +1168,25 @@ route. With this information, any URL can easily be generated::
1168
1168
1169
1169
..note ::
1170
1170
1171
- In controllers that extend Symfony's base
1171
+ In controllers thatdon't extend Symfony's base
1172
1172
: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
+ }
1177
1190
1178
1191
In an upcoming section, you'll learn how to generate URLs from inside templates.
1179
1192
@@ -1262,19 +1275,19 @@ to ``generateUrl()``:
1262
1275
1263
1276
..note ::
1264
1277
1265
- The host that's used when generating an absolute URL isthe host of
1266
- the current ``Request `` object.This is detected automatically. But if
1267
- you generate absolute URLs forscripts 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 isautomatically
1279
+ detected using the current ``Request `` object.When generating absolute
1280
+ URLs from outside the web context ( forinstance in a console command) this
1281
+ doesn 't work.See :doc: `/cookbook/console/sending_emails ` to learn how to
1282
+ solve this problem .
1270
1283
1271
1284
Summary
1272
1285
-------
1273
1286
1274
1287
Routing is a system for mapping the URL of incoming requests to the controller
1275
1288
function that should be called to process the request. It both allows you
1276
1289
to specify beautiful URLs and keeps the functionality of your application
1277
- decoupled from those URLs. Routing is atwo-way mechanism, meaning that it
1290
+ decoupled from those URLs. Routing is abidirectional mechanism, meaning that it
1278
1291
should also be used to generate URLs.
1279
1292
1280
1293
Learn more from the Cookbook