21
21
22
22
In order to set up a basic routing system you need three parts:
23
23
24
- * A ` :class:Symfony\\Component\\Routing\\RouteCollection `, which contains the route definitions (instances of the class ` :class:Symfony\\Component\\Routing\\Route `)
25
- * A ` :class:Symfony\\Component\\Routing\\RequestContext `, which has information about the request
26
- * A ` :class:Symfony\\Component\\Routing\\Matcher\\UrlMatcher `, which performs the mapping of the request to a single route
24
+ * A :class: ` Symfony\\ Component\\ Routing\\ RouteCollection `, which contains the route definitions (instances of the class :class: ` Symfony\\ Component\\ Routing\\ Route `)
25
+ * A :class: ` Symfony\\ Component\\ Routing\\ RequestContext `, which has information about the request
26
+ * A :class: ` Symfony\\ Component\\ Routing\\ Matcher\\ UrlMatcher `, which performs the mapping of the request to a single route
27
27
28
28
.. code-block :: php
29
29
@@ -43,20 +43,20 @@ In order to set up a basic routing system you need three parts:
43
43
// array('controller' => 'MyController', '_route' => 'route_name')
44
44
45
45
You can add as many routes as you like to a
46
- ` :class:Symfony\\Component\\Routing\\RouteCollection `. The first argument you have to
47
- provide to the ` :method:Symfony\\Component\\Routing\\RouteCollection::add ` method is
48
- the name of the Route. The constructor of the ` :class:Symfony\\Component\\Routing\\Route `
46
+ :class: ` Symfony\\ Component\\ Routing\\ RouteCollection `. The first argument you have to
47
+ provide to the :method: ` Symfony\\ Component\\ Routing\\ RouteCollection::add ` method is
48
+ the name of the Route. The constructor of the :class: ` Symfony\\ Component\\ Routing\\ Route `
49
49
class expects an url path and an array of custom variables. These are returned by the
50
50
Matcher if the route matches the request.
51
51
52
52
If no matching route can be found a
53
- ` :class:Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException ` will be thrown.
53
+ :class: ` Symfony\\ Component\\ Routing\\ Exception\\ ResourceNotFoundException ` will be thrown.
54
54
55
55
Additionally to the custom variables the name of the route is added with the
56
56
key "_route".
57
57
58
58
Defining routes
59
- ~~~~~~~~
59
+ ~~~~~~~~~~~~~~~
60
60
61
61
A route contains:
62
62
@@ -79,25 +79,25 @@ A route contains:
79
79
$parameters = $matcher->match('/archive/2012-01');
80
80
// array('controller' => 'showArchive', 'month' => 2012-01'', '_route' => '...')
81
81
82
- $parameters = $matcher->match( '/archive/foo' );
82
+ $parameters = $matcher->match('/archive/foo');
83
83
// throws ResourceNotFoundException
84
84
85
85
Besides the regular expression constraints there are two special requirements
86
86
you can define:
87
87
88
88
* ``_method `` enforces a certain HTTP request method (``HEAD ``, ``GET ``, ``POST ``, ...)
89
- * ``_scheme `` enforces a certian HTTP scheme (``http ``, ``https ``)
89
+ * ``_scheme `` enforces a certain HTTP scheme (``http ``, ``https ``)
90
90
91
91
.. code-block :: php
92
92
93
93
// Only accepts requests to /foo with the POST method and a secure connection.
94
94
$route = new Route('/foo', array('_method' => 'post', '_scheme' => 'https' ));
95
95
96
96
Using prefixes
97
- ~~~~~~~~
97
+ ~~~~~~~~~~~~~~
98
98
99
99
You can add routes or other instances of
100
- ` :class:Symfony\\Component\\Routing\\RouteCollection ` to a collection. This way
100
+ :class: ` Symfony\\ Component\\ Routing\\ RouteCollection ` to a collection. This way
101
101
you can build a tree of routes. Additionally you can define a prefix, default
102
102
requirements and default options to all routes of a subtree:
103
103
@@ -112,19 +112,20 @@ requirements and default options to all routes of a subtree:
112
112
$rootCollection->addCollection($subCollection, '/prefix', array('_scheme' => 'https'));
113
113
114
114
Set the request parameters
115
- ~~~~~~~~
115
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
116
116
117
- The ` :class:Symfony\\Component\\Routing\\RequestContext ` provides information
117
+ The :class: ` Symfony\\ Component\\ Routing\\ RequestContext ` provides information
118
118
about a request. You can define all parameters of a HTTP request with this class:
119
119
120
120
.. code-block :: php
121
121
122
122
public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443)
123
123
124
124
Normally you can pass the values from the ``$_SERVER `` variable variable to the
125
- `:class:Symfony\\Component\\Routing\\RequestContext `. If you use Symfony you can
126
- use it's `:class:Symfony\\Component\\HttpFoundation\\Request ` object to feed the
127
- `:class:Symfony\\Component\\Routing\\RequestContext ` in a shortcut:
125
+ :class: `Symfony\\ Component\\ Routing\\ RequestContext `. If you use the
126
+ :doc: `HttpFoundation<http_foundation> ` component you can use it's
127
+ :class: `Symfony\\ Component\\ HttpFoundation\\ Request ` class to feed the
128
+ :class: `Symfony\\ Component\\ Routing\\ RequestContext ` in a shortcut:
128
129
129
130
.. code-block :: php
130
131
@@ -134,9 +135,9 @@ use it's `:class:Symfony\\Component\\HttpFoundation\\Request` object to feed the
134
135
$context->fromRequest(Request::createFromGlobals());
135
136
136
137
Generate a URL
137
- ~~~~~~~~
138
+ ~~~~~~~~~~~~~~
138
139
139
- While the ` :class:Symfony\\Component\\Routing\\Matcher\\UrlMatcher ` tries to find
140
+ While the :class: ` Symfony\\ Component\\ Routing\\ Matcher\\ UrlMatcher ` tries to find
140
141
a route that fits the given request you can also build an URL from a certain route:
141
142
142
143
.. code-block :: php
@@ -155,21 +156,19 @@ a route that fits the given request you can also build an URL from a certain rou
155
156
156
157
.. note ::
157
158
158
- An absolute URL is generated if you have defined a ``_scheme `` requirement for
159
- the matched route.
159
+ If you have defined the ``_scheme `` requirement an absolute URL is generated
160
+ if the scheme of the current :class: `Symfony\\ Component\\ Routing\\ RequestContext `
161
+ isn't the same.
160
162
161
163
Load routes from a file
162
- ~~~~~~~~
164
+ ~~~~~~~~~~~~~~~~~~~~~~~
163
165
164
- There is a number of loader classes. They give you the abbility to load a collection
165
- of route definitions from external files. There are:
166
-
167
- * `:class:Symfony\\Component\\Routing\\Loader\\XmlFileLoader `
168
- * `:class:Symfony\\Component\\Routing\\Loader\\YamlFileLoader `
169
- * `:class:Symfony\\Component\\Routing\\Loader\\PhpFileLoader `
170
- * and others
171
-
172
- Here is an example with `:class:Symfony\\Component\\Routing\\Loader\\YamlFileLoader `:
166
+ There is a number of loader classes. They give you the ability to load a collection
167
+ of route definitions from external files.
168
+ The loaders expect a :class: `Symfony\\ Component\\ Config\\ FileLocator ` as the
169
+ constructor argument. You can use the :class: `Symfony\\ Component\\ Config\\ FileLocator `
170
+ to define an array of paths in which the loader will look for the requested files.
171
+ If the file could be found the loader returns a :class: `Symfony\\ Component\\ Routing\\ RouteCollection `.
173
172
174
173
.. code-block :: yaml
175
174
@@ -184,13 +183,57 @@ Here is an example with `:class:Symfony\\Component\\Routing\\Loader\\YamlFileLoa
184
183
185
184
.. code-block :: php
186
185
187
- $loader = new YamlFileLoader();
186
+ use Symfony\Component\Config\FileLocator;
187
+ use Symfony\Component\Routing\Loader\YamlFileLoader;
188
+
189
+ $locator = new FileLocator(array(__DIR__));
190
+ $loader = new YamlFileLoader($locator);
188
191
$collection = $loader->load('routes.yml');
189
192
193
+ Besides :class: `Symfony\\ Component\\ Routing\\ Loader\\ YamlFileLoader ` there are two
194
+ other loaders that work the same way:
195
+
196
+ * :class: `Symfony\\ Component\\ Routing\\ Loader\\ XmlFileLoader `
197
+ * :class: `Symfony\\ Component\\ Routing\\ Loader\\ PhpFileLoader `
198
+
199
+ If you use the :class: `Symfony\\ Component\\ Routing\\ Loader\\ PhpFileLoader ` you
200
+ have to provide the name of a php file which returns a :class: `Symfony\\ Component\\ Routing\\ RouteCollection `:
201
+
202
+ .. code-block :: php
203
+
204
+ // RouteProvider.php
205
+ use Symfony\Component\Routing\RouteCollection;
206
+ use Symfony\Component\Routing\Route;
207
+
208
+ $collection = new RouteCollection();
209
+ $collection->add('route_name', new Route('/foo', array('controller' => 'ExampleController')));
210
+ // ...
211
+
212
+ return $collection;
213
+
214
+ There is also the :class: `Symfony\\ Component\\ Routing\\ Loader\\ ClosureLoader `, which
215
+ calls a closure and uses the result as a :class: `Symfony\\ Component\\ Routing\\ RouteCollection `:
216
+
217
+ .. code-block :: php
218
+
219
+ use Symfony\Component\Routing\Loader\ClosureLoader;
220
+
221
+ $closure = function() {
222
+ return new RouteCollection();
223
+ };
224
+
225
+ $loader = new ClosureLoader();
226
+ $collection = $loader->load($closure);
227
+
228
+ Last but not least there are
229
+ :class: `Symfony\\ Component\\ Routing\\ Loader\\ AnnotationDirectoryLoader ` and
230
+ :class: `Symfony\\ Component\\ Routing\\ Loader\\ AnnotationFileLoader ` to load
231
+ route definitions from class annotations.
232
+
190
233
The all-in-one Router
191
- ~~~~~~~~
234
+ ~~~~~~~~~~~~~~~~~~~~~
192
235
193
- The ` :class:Symfony\\Component\\Routing\\Router ` class is a all-in-one package of
236
+ The :class: ` Symfony\\ Component\\ Routing\\ Router ` class is a all-in-one package of
194
237
the routing algorithm. The constructor expects a loader instance, a path to the
195
238
main route definition and some other settings:
196
239
@@ -201,11 +244,12 @@ main route definition and some other settings:
201
244
With the ``cache_dir `` option you can enable route caching (if you provide a
202
245
path) or disable caching (if it's set to ``null ``). The caching is done
203
246
automatically in the background if you want to use it. A basic example of the
204
- ` :class:Symfony\\Component\\Routing\\Router ` class would look like:
247
+ :class: ` Symfony\\ Component\\ Routing\\ Router ` class would look like:
205
248
206
249
.. code-block :: php
207
250
208
- $router = new Router(new YamlFileLoader(), "routes.yml");
251
+ $locator = new FileLocator(array(__DIR__));
252
+ $router = new Router(new YamlFileLoader($locator), "routes.yml");
209
253
$router->match('/foo/bar');
210
254
211
255
.. note ::
0 commit comments