Skip to content

Commit 97ff5a9

Browse files
sebhoerlweaverryan
authored andcommitted
Updated documentation
1 parent b025ac1 commit 97ff5a9

File tree

1 file changed

+80
-36
lines changed

1 file changed

+80
-36
lines changed

components/routing.rst

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Usage
2121

2222
In order to set up a basic routing system you need three parts:
2323

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
2727

2828
.. code-block:: php
2929
@@ -43,20 +43,20 @@ In order to set up a basic routing system you need three parts:
4343
// array('controller' => 'MyController', '_route' => 'route_name')
4444
4545
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`
4949
class expects an url path and an array of custom variables. These are returned by the
5050
Matcher if the route matches the request.
5151

5252
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.
5454

5555
Additionally to the custom variables the name of the route is added with the
5656
key "_route".
5757

5858
Defining routes
59-
~~~~~~~~
59+
~~~~~~~~~~~~~~~
6060

6161
A route contains:
6262

@@ -79,25 +79,25 @@ A route contains:
7979
$parameters = $matcher->match('/archive/2012-01');
8080
// array('controller' => 'showArchive', 'month' => 2012-01'', '_route' => '...')
8181
82-
$parameters = $matcher->match( '/archive/foo' );
82+
$parameters = $matcher->match('/archive/foo');
8383
// throws ResourceNotFoundException
8484
8585
Besides the regular expression constraints there are two special requirements
8686
you can define:
8787

8888
* ``_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``)
9090

9191
.. code-block:: php
9292
9393
// Only accepts requests to /foo with the POST method and a secure connection.
9494
$route = new Route('/foo', array('_method' => 'post', '_scheme' => 'https' ));
9595
9696
Using prefixes
97-
~~~~~~~~
97+
~~~~~~~~~~~~~~
9898

9999
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
101101
you can build a tree of routes. Additionally you can define a prefix, default
102102
requirements and default options to all routes of a subtree:
103103

@@ -112,19 +112,20 @@ requirements and default options to all routes of a subtree:
112112
$rootCollection->addCollection($subCollection, '/prefix', array('_scheme' => 'https'));
113113
114114
Set the request parameters
115-
~~~~~~~~
115+
~~~~~~~~~~~~~~~~~~~~~~~~~~
116116

117-
The `:class:Symfony\\Component\\Routing\\RequestContext` provides information
117+
The :class:`Symfony\\Component\\Routing\\RequestContext` provides information
118118
about a request. You can define all parameters of a HTTP request with this class:
119119

120120
.. code-block:: php
121121
122122
public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443)
123123
124124
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:
128129

129130
.. code-block:: php
130131
@@ -134,9 +135,9 @@ use it's `:class:Symfony\\Component\\HttpFoundation\\Request` object to feed the
134135
$context->fromRequest(Request::createFromGlobals());
135136
136137
Generate a URL
137-
~~~~~~~~
138+
~~~~~~~~~~~~~~
138139

139-
While the `:class:Symfony\\Component\\Routing\\Matcher\\UrlMatcher` tries to find
140+
While the :class:`Symfony\\Component\\Routing\\Matcher\\UrlMatcher` tries to find
140141
a route that fits the given request you can also build an URL from a certain route:
141142

142143
.. code-block:: php
@@ -155,21 +156,19 @@ a route that fits the given request you can also build an URL from a certain rou
155156
156157
.. note::
157158

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.
160162

161163
Load routes from a file
162-
~~~~~~~~
164+
~~~~~~~~~~~~~~~~~~~~~~~
163165

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`.
173172

174173
.. code-block:: yaml
175174
@@ -184,13 +183,57 @@ Here is an example with `:class:Symfony\\Component\\Routing\\Loader\\YamlFileLoa
184183
185184
.. code-block:: php
186185
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);
188191
$collection = $loader->load('routes.yml');
189192
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+
190233
The all-in-one Router
191-
~~~~~~~~
234+
~~~~~~~~~~~~~~~~~~~~~
192235

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
194237
the routing algorithm. The constructor expects a loader instance, a path to the
195238
main route definition and some other settings:
196239

@@ -201,11 +244,12 @@ main route definition and some other settings:
201244
With the ``cache_dir`` option you can enable route caching (if you provide a
202245
path) or disable caching (if it's set to ``null``). The caching is done
203246
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:
205248

206249
.. code-block:: php
207250
208-
$router = new Router(new YamlFileLoader(), "routes.yml");
251+
$locator = new FileLocator(array(__DIR__));
252+
$router = new Router(new YamlFileLoader($locator), "routes.yml");
209253
$router->match('/foo/bar');
210254
211255
.. note::

0 commit comments

Comments
 (0)