@@ -39,8 +39,8 @@ your autoloader to load the Routing component::
39
39
40
40
use Symfony\Component\Routing\Matcher\UrlMatcher;
41
41
use Symfony\Component\Routing\RequestContext;
42
- use Symfony\Component\Routing\RouteCollection;
43
42
use Symfony\Component\Routing\Route;
43
+ use Symfony\Component\Routing\RouteCollection;
44
44
45
45
$route = new Route('/foo', ['_controller' => 'MyController']);
46
46
$routes = new RouteCollection();
@@ -82,20 +82,24 @@ be thrown.
82
82
Defining Routes
83
83
~~~~~~~~~~~~~~~
84
84
85
- A full route definition can contain up to seven parts:
85
+ A full route definition can contain up to eight parts:
86
86
87
- #. The URL path route. This is matched against the URL passed to the `RequestContext `,
88
- and can contain named wildcard placeholders (e.g. ``{placeholders} ``)
89
- to match dynamic parts in the URL.
87
+ #. The URL pattern. This is matched against the URL passed to the
88
+ ``RequestContext ``. It is not a regular expression, but can contain named
89
+ wildcard placeholders (e.g. ``{slug} ``) to match dynamic parts in the URL.
90
+ The component will create the regular expression from it.
90
91
91
- #. An array of default values. This contains an array of arbitrary values
92
- that will be returned when the request matches the route.
92
+ #. An array of default parameters. This contains an array of arbitrary values
93
+ that will be returned when the request matches the route. It is used by
94
+ convention to map a controller to the route.
93
95
94
96
#. An array of requirements. These define constraints for the values of the
95
- placeholders as regular expressions.
97
+ placeholders in the pattern as regular expressions.
96
98
97
- #. An array of options. These contain internal settings for the route and
98
- are the least commonly needed.
99
+ #. An array of options. These contain advanced settings for the route and
100
+ can be used to control encoding or customize compilation.
101
+ See :ref: `routing-unicode-support ` below. You can learn more about them by
102
+ reading :method: `Symfony\\ Component\\ Routing\\ Route::setOptions ` implementation.
99
103
100
104
#. A host. This is matched against the host of the request. See
101
105
:doc: `/routing/hostname_pattern ` for more details.
@@ -105,6 +109,10 @@ A full route definition can contain up to seven parts:
105
109
#. An array of methods. These enforce a certain HTTP request method (``HEAD ``,
106
110
``GET ``, ``POST ``, ...).
107
111
112
+ #. A condition, using the :doc: `/components/expression_language/syntax `.
113
+ A string that must evaluate to ``true `` so the route matches. See
114
+ :doc: `/routing/conditions ` for more details.
115
+
108
116
Take the following route, which combines several of these ideas::
109
117
110
118
$route = new Route(
@@ -114,7 +122,8 @@ Take the following route, which combines several of these ideas::
114
122
[], // options
115
123
'{subdomain}.example.com', // host
116
124
[], // schemes
117
- [] // methods
125
+ [], // methods
126
+ 'context.getHost() matches "/(secure|admin).example.com/"' // condition
118
127
);
119
128
120
129
// ...
@@ -138,19 +147,22 @@ When using wildcards, these are returned in the array result when calling
138
147
``match ``. The part of the path that the wildcard matched (e.g. ``2012-01 ``) is used
139
148
as value.
140
149
141
- .. tip ::
150
+ A placeholder matches any character except slashes ``/ `` by default, unless you define
151
+ a specific requirement for it.
152
+ The reason is that they are used by convention to separate different placeholders.
142
153
143
- If you want to match all URLs which start with a certain path and end in an
144
- arbitrary suffix you can use the following route definition::
154
+ If you want a placeholder to match anything, it must be the last of the route::
145
155
146
- $route = new Route(
147
- '/start/{suffix }',
148
- ['suffix ' => ''],
149
- ['suffix ' => '.*']
150
- );
156
+ $route = new Route(
157
+ '/start/{required}/{anything }',
158
+ ['required ' => 'default '], // should always be defined
159
+ ['anything ' => '.*'] // explicit requirement to allow "/"
160
+ );
151
161
152
- Using Prefixes
153
- ~~~~~~~~~~~~~~
162
+ Learn more about it by reading :ref: `routing/slash_in_parameter `.
163
+
164
+ Using Prefixes and Collection Settings
165
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154
166
155
167
You can add routes or other instances of
156
168
:class: `Symfony\\ Component\\ Routing\\ RouteCollection ` to *another * collection.
@@ -166,11 +178,12 @@ host to all routes of a subtree using methods provided by the
166
178
$subCollection->add(...);
167
179
$subCollection->addPrefix('/prefix');
168
180
$subCollection->addDefaults([...]);
169
- $subCollection->addRequirements([]);
170
- $subCollection->addOptions([]);
171
- $subCollection->setHost('admin .example.com');
181
+ $subCollection->addRequirements([... ]);
182
+ $subCollection->addOptions([... ]);
183
+ $subCollection->setHost('{subdomain} .example.com');
172
184
$subCollection->setMethods(['POST']);
173
185
$subCollection->setSchemes(['https']);
186
+ $subCollection->setCondition('context.getHost() matches "/(secure|admin).example.com/"');
174
187
175
188
$rootCollection->addCollection($subCollection);
176
189
@@ -210,7 +223,7 @@ Generate a URL
210
223
211
224
While the :class: `Symfony\\ Component\\ Routing\\ Matcher\\ UrlMatcher ` tries
212
225
to find a route that fits the given request you can also build a URL from
213
- a certain route::
226
+ a certain route with the :class: ` Symfony \\ Component \\ Routing \\ Generator \\ UrlGenerator ` ::
214
227
215
228
use Symfony\Component\Routing\Generator\UrlGenerator;
216
229
use Symfony\Component\Routing\RequestContext;
@@ -260,7 +273,7 @@ when the route doesn't exist::
260
273
Load Routes from a File
261
274
~~~~~~~~~~~~~~~~~~~~~~~
262
275
263
- You've already seen how you can easily add routes to a collection right inside
276
+ You've already seen how you can add routes to a collection right inside
264
277
PHP. But you can also load routes from a number of different files.
265
278
266
279
The Routing component comes with a number of loader classes, each giving
@@ -347,7 +360,7 @@ The all-in-one Router
347
360
~~~~~~~~~~~~~~~~~~~~~
348
361
349
362
The :class: `Symfony\\ Component\\ Routing\\ Router ` class is an all-in-one package
350
- to quickly use the Routing component. The constructor expects a loader instance,
363
+ to use the Routing component. The constructor expects a loader instance,
351
364
a path to the main route definition and some other settings::
352
365
353
366
public function __construct(
@@ -372,14 +385,17 @@ automatically in the background if you want to use it. A basic example of the
372
385
['cache_dir' => __DIR__.'/cache'],
373
386
$requestContext
374
387
);
375
- $router->match('/foo/bar');
388
+ $parameters = $router->match('/foo/bar');
389
+ $url = $router->generate('some_route', ['parameter' => 'value']);
376
390
377
391
.. note ::
378
392
379
393
If you use caching, the Routing component will compile new classes which
380
394
are saved in the ``cache_dir ``. This means your script must have write
381
395
permissions for that location.
382
396
397
+ .. _routing-unicode-support :
398
+
383
399
Unicode Routing Support
384
400
~~~~~~~~~~~~~~~~~~~~~~~
385
401
@@ -481,7 +497,7 @@ You can also include UTF-8 strings as routing requirements:
481
497
* @Route(
482
498
* "/category/{name}",
483
499
* name="route2",
484
- * requirements ={"default"= "한국어"},
500
+ * defaults ={"name": "한국어"},
485
501
* options={"utf8": true}
486
502
* )
487
503
*/
@@ -494,10 +510,10 @@ You can also include UTF-8 strings as routing requirements:
494
510
495
511
# app/config/routing.yml
496
512
route2 :
497
- path : /default/{default }
498
- defaults : { _controller: 'AppBundle:Default:default' }
499
- requirements :
500
- default : " 한국어"
513
+ path : /category/{name }
514
+ defaults :
515
+ _controller : ' AppBundle:Default:default '
516
+ name : ' 한국어'
501
517
options :
502
518
utf8 : true
503
519
@@ -510,9 +526,9 @@ You can also include UTF-8 strings as routing requirements:
510
526
xsi : schemaLocation =" http://symfony.com/schema/routing
511
527
https://symfony.com/schema/routing/routing-1.0.xsd" >
512
528
513
- <route id =" route2" path =" /default/{default }" >
529
+ <route id =" route2" path =" /category/{name }" >
514
530
<default key =" _controller" >AppBundle:Default:default</default >
515
- <requirement key =" default " >한국어</requirement >
531
+ <default key =" name " >한국어</requirement >
516
532
<option key =" utf8" >true</option >
517
533
</route >
518
534
</routes >
@@ -527,10 +543,9 @@ You can also include UTF-8 strings as routing requirements:
527
543
$routes->add('route2', new Route('/default/{default}',
528
544
[
529
545
'_controller' => 'AppBundle:Default:default',
546
+ 'name' => '한국어',
530
547
],
531
- [
532
- 'default' => '한국어',
533
- ],
548
+ [],
534
549
[
535
550
'utf8' => true,
536
551
]
0 commit comments