5
5
The Routing Component
6
6
=====================
7
7
8
- The Routing Component maps an HTTP request to a set of configuration
8
+ The Routing Component maps an HTTP request to a set of configuration
9
9
variables.
10
10
11
11
Installation
@@ -34,14 +34,15 @@ your autoloader to load the Routing component::
34
34
use Symfony\Component\Routing\RouteCollection;
35
35
use Symfony\Component\Routing\Route;
36
36
37
+ $route = new Route('/foo', array('controller' => 'MyController'))
37
38
$routes = new RouteCollection();
38
- $routes->add('route_name', new Route('/foo', array('controller' => 'MyController')) );
39
+ $routes->add('route_name', $route );
39
40
40
41
$context = new RequestContext($_SERVER['REQUEST_URI']);
41
42
42
43
$matcher = new UrlMatcher($routes, $context);
43
44
44
- $parameters = $matcher->match('/foo');
45
+ $parameters = $matcher->match('/foo');
45
46
// array('controller' => 'MyController', '_route' => 'route_name')
46
47
47
48
.. note ::
@@ -51,7 +52,7 @@ your autoloader to load the Routing component::
51
52
matching. An easy way to solve this is to use the HttpFoundation component
52
53
as explained :ref: `below<components-routing-http-foundation> `.
53
54
54
- You can add as many routes as you like to a
55
+ You can add as many routes as you like to a
55
56
:class: `Symfony\\ Component\\ Routing\\ RouteCollection `.
56
57
57
58
The :method: `RouteCollection::add()<Symfony\\ Component\\ Routing\\ RouteCollection::add> `
@@ -61,7 +62,7 @@ URL path and some array of custom variables in its constructor. This array
61
62
of custom variables can be *anything * that's significant to your application,
62
63
and is returned when that route is matched.
63
64
64
- If no matching route can be found a
65
+ If no matching route can be found a
65
66
:class: `Symfony\\ Component\\ Routing\\ Exception\\ ResourceNotFoundException ` will be thrown.
66
67
67
68
In addition to your array of custom variables, a ``_route `` key is added,
@@ -97,7 +98,11 @@ Take the following route, which combines several of these ideas::
97
98
// ...
98
99
99
100
$parameters = $matcher->match('/archive/2012-01');
100
- // array('controller' => 'showArchive', 'month' => '2012-01', '_route' => ...)
101
+ // array(
102
+ // 'controller' => 'showArchive',
103
+ // 'month' => '2012-01',
104
+ // '_route' => ...
105
+ // )
101
106
102
107
$parameters = $matcher->match('/archive/foo');
103
108
// throws ResourceNotFoundException
@@ -106,29 +111,37 @@ In this case, the route is matched by ``/archive/2012-01``, because the ``{month
106
111
wildcard matches the regular expression wildcard given. However, ``/archive/foo ``
107
112
does *not * match, because "foo" fails the month wildcard.
108
113
109
- Besides the regular expression constraints there are two special requirements
114
+ Besides the regular expression constraints there are two special requirements
110
115
you can define:
111
116
112
117
* ``_method `` enforces a certain HTTP request method (``HEAD ``, ``GET ``, ``POST ``, ...)
113
- * ``_scheme `` enforces a certain HTTP scheme (``http ``, ``https ``)
118
+ * ``_scheme `` enforces a certain HTTP scheme (``http ``, ``https ``)
114
119
115
120
For example, the following route would only accept requests to /foo with
116
121
the POST method and a secure connection::
117
122
118
- $route = new Route('/foo', array(), array('_method' => 'post', '_scheme' => 'https' ));
123
+ $route = new Route(
124
+ '/foo',
125
+ array(),
126
+ array('_method' => 'post', '_scheme' => 'https' )
127
+ );
119
128
120
129
.. tip ::
121
-
130
+
122
131
If you want to match all urls which start with a certain path and end in an
123
132
arbitrary suffix you can use the following route definition::
124
-
125
- $route = new Route('/start/{suffix}', array('suffix' => ''), array('suffix' => '.*'));
126
-
133
+
134
+ $route = new Route(
135
+ '/start/{suffix}',
136
+ array('suffix' => ''),
137
+ array('suffix' => '.*')
138
+ );
139
+
127
140
128
141
Using Prefixes
129
142
~~~~~~~~~~~~~~
130
143
131
- You can add routes or other instances of
144
+ You can add routes or other instances of
132
145
:class: `Symfony\\ Component\\ Routing\\ RouteCollection ` to *another * collection.
133
146
This way you can build a tree of routes. Additionally you can define a prefix,
134
147
default requirements and default options to all routes of a subtree::
@@ -139,23 +152,34 @@ default requirements and default options to all routes of a subtree::
139
152
$subCollection->add(...);
140
153
$subCollection->add(...);
141
154
142
- $rootCollection->addCollection($subCollection, '/prefix', array('_scheme' => 'https'));
155
+ $rootCollection->addCollection(
156
+ $subCollection,
157
+ '/prefix',
158
+ array('_scheme' => 'https')
159
+ );
143
160
144
161
Set the Request Parameters
145
162
~~~~~~~~~~~~~~~~~~~~~~~~~~
146
163
147
- The :class: `Symfony\\ Component\\ Routing\\ RequestContext ` provides information
164
+ The :class: `Symfony\\ Component\\ Routing\\ RequestContext ` provides information
148
165
about the current request. You can define all parameters of an HTTP request
149
166
with this class via its constructor::
150
167
151
- public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443)
168
+ public function __construct(
169
+ $baseUrl = '',
170
+ $method = 'GET',
171
+ $host = 'localhost',
172
+ $scheme = 'http',
173
+ $httpPort = 80,
174
+ $httpsPort = 443
175
+ )
152
176
153
177
.. _components-routing-http-foundation :
154
178
155
- Normally you can pass the values from the ``$_SERVER `` variable to populate the
179
+ Normally you can pass the values from the ``$_SERVER `` variable to populate the
156
180
:class: `Symfony\\ Component\\ Routing\\ RequestContext `. But If you use the
157
- :doc: `HttpFoundation</components/http_foundation/index> ` component, you can use its
158
- :class: `Symfony\\ Component\\ HttpFoundation\\ Request ` class to feed the
181
+ :doc: `HttpFoundation</components/http_foundation/index> ` component, you can use its
182
+ :class: `Symfony\\ Component\\ HttpFoundation\\ Request ` class to feed the
159
183
:class: `Symfony\\ Component\\ Routing\\ RequestContext ` in a shortcut::
160
184
161
185
use Symfony\Component\HttpFoundation\Request;
@@ -242,15 +266,18 @@ have to provide the name of a php file which returns a :class:`Symfony\\Componen
242
266
use Symfony\Component\Routing\Route;
243
267
244
268
$collection = new RouteCollection();
245
- $collection->add('route_name', new Route('/foo', array('controller' => 'ExampleController')));
269
+ $collection->add(
270
+ 'route_name',
271
+ new Route('/foo', array('controller' => 'ExampleController'))
272
+ );
246
273
// ...
247
274
248
275
return $collection;
249
276
250
277
Routes as Closures
251
278
..................
252
279
253
- There is also the :class: `Symfony\\ Component\\ Routing\\ Loader\\ ClosureLoader `, which
280
+ There is also the :class: `Symfony\\ Component\\ Routing\\ Loader\\ ClosureLoader `, which
254
281
calls a closure and uses the result as a :class: `Symfony\\ Component\\ Routing\\ RouteCollection `::
255
282
256
283
use Symfony\Component\Routing\Loader\ClosureLoader;
@@ -278,11 +305,17 @@ The :class:`Symfony\\Component\\Routing\\Router` class is a all-in-one package
278
305
to quickly use the Routing component. The constructor expects a loader instance,
279
306
a path to the main route definition and some other settings::
280
307
281
- public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, array $defaults = array());
308
+ public function __construct(
309
+ LoaderInterface $loader,
310
+ $resource,
311
+ array $options = array(),
312
+ RequestContext $context = null,
313
+ array $defaults = array()
314
+ );
282
315
283
- With the ``cache_dir `` option you can enable route caching (if you provide a
284
- path) or disable caching (if it's set to ``null ``). The caching is done
285
- automatically in the background if you want to use it. A basic example of the
316
+ With the ``cache_dir `` option you can enable route caching (if you provide a
317
+ path) or disable caching (if it's set to ``null ``). The caching is done
318
+ automatically in the background if you want to use it. A basic example of the
286
319
:class: `Symfony\\ Component\\ Routing\\ Router ` class would look like::
287
320
288
321
$locator = new FileLocator(array(__DIR__));
@@ -298,6 +331,6 @@ automatically in the background if you want to use it. A basic example of the
298
331
299
332
.. note ::
300
333
301
- If you use caching, the Routing component will compile new classes which
302
- are saved in the ``cache_dir ``. This means your script must have write
334
+ If you use caching, the Routing component will compile new classes which
335
+ are saved in the ``cache_dir ``. This means your script must have write
303
336
permissions for that location.
0 commit comments