Skip to content

Commit bcff076

Browse files
Reducing line length to avoid horizontal scrolling in Routing examples
1 parent 008f1e1 commit bcff076

File tree

1 file changed

+61
-28
lines changed

1 file changed

+61
-28
lines changed

components/routing.rst

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
The Routing Component
66
=====================
77

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
99
variables.
1010

1111
Installation
@@ -34,14 +34,15 @@ your autoloader to load the Routing component::
3434
use Symfony\Component\Routing\RouteCollection;
3535
use Symfony\Component\Routing\Route;
3636

37+
$route = new Route('/foo', array('controller' => 'MyController'))
3738
$routes = new RouteCollection();
38-
$routes->add('route_name', new Route('/foo', array('controller' => 'MyController')));
39+
$routes->add('route_name', $route);
3940

4041
$context = new RequestContext($_SERVER['REQUEST_URI']);
4142

4243
$matcher = new UrlMatcher($routes, $context);
4344

44-
$parameters = $matcher->match('/foo');
45+
$parameters = $matcher->match('/foo');
4546
// array('controller' => 'MyController', '_route' => 'route_name')
4647

4748
.. note::
@@ -51,7 +52,7 @@ your autoloader to load the Routing component::
5152
matching. An easy way to solve this is to use the HttpFoundation component
5253
as explained :ref:`below<components-routing-http-foundation>`.
5354

54-
You can add as many routes as you like to a
55+
You can add as many routes as you like to a
5556
:class:`Symfony\\Component\\Routing\\RouteCollection`.
5657

5758
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
6162
of custom variables can be *anything* that's significant to your application,
6263
and is returned when that route is matched.
6364

64-
If no matching route can be found a
65+
If no matching route can be found a
6566
:class:`Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException` will be thrown.
6667

6768
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::
9798
// ...
9899

99100
$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+
// )
101106

102107
$parameters = $matcher->match('/archive/foo');
103108
// throws ResourceNotFoundException
@@ -106,29 +111,37 @@ In this case, the route is matched by ``/archive/2012-01``, because the ``{month
106111
wildcard matches the regular expression wildcard given. However, ``/archive/foo``
107112
does *not* match, because "foo" fails the month wildcard.
108113

109-
Besides the regular expression constraints there are two special requirements
114+
Besides the regular expression constraints there are two special requirements
110115
you can define:
111116

112117
* ``_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``)
114119

115120
For example, the following route would only accept requests to /foo with
116121
the POST method and a secure connection::
117122

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+
);
119128

120129
.. tip::
121-
130+
122131
If you want to match all urls which start with a certain path and end in an
123132
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+
127140

128141
Using Prefixes
129142
~~~~~~~~~~~~~~
130143

131-
You can add routes or other instances of
144+
You can add routes or other instances of
132145
:class:`Symfony\\Component\\Routing\\RouteCollection` to *another* collection.
133146
This way you can build a tree of routes. Additionally you can define a prefix,
134147
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::
139152
$subCollection->add(...);
140153
$subCollection->add(...);
141154

142-
$rootCollection->addCollection($subCollection, '/prefix', array('_scheme' => 'https'));
155+
$rootCollection->addCollection(
156+
$subCollection,
157+
'/prefix',
158+
array('_scheme' => 'https')
159+
);
143160

144161
Set the Request Parameters
145162
~~~~~~~~~~~~~~~~~~~~~~~~~~
146163

147-
The :class:`Symfony\\Component\\Routing\\RequestContext` provides information
164+
The :class:`Symfony\\Component\\Routing\\RequestContext` provides information
148165
about the current request. You can define all parameters of an HTTP request
149166
with this class via its constructor::
150167

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+
)
152176

153177
.. _components-routing-http-foundation:
154178

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
156180
: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
159183
:class:`Symfony\\Component\\Routing\\RequestContext` in a shortcut::
160184

161185
use Symfony\Component\HttpFoundation\Request;
@@ -242,15 +266,18 @@ have to provide the name of a php file which returns a :class:`Symfony\\Componen
242266
use Symfony\Component\Routing\Route;
243267

244268
$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+
);
246273
// ...
247274

248275
return $collection;
249276

250277
Routes as Closures
251278
..................
252279

253-
There is also the :class:`Symfony\\Component\\Routing\\Loader\\ClosureLoader`, which
280+
There is also the :class:`Symfony\\Component\\Routing\\Loader\\ClosureLoader`, which
254281
calls a closure and uses the result as a :class:`Symfony\\Component\\Routing\\RouteCollection`::
255282

256283
use Symfony\Component\Routing\Loader\ClosureLoader;
@@ -278,11 +305,17 @@ The :class:`Symfony\\Component\\Routing\\Router` class is a all-in-one package
278305
to quickly use the Routing component. The constructor expects a loader instance,
279306
a path to the main route definition and some other settings::
280307

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+
);
282315

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
286319
:class:`Symfony\\Component\\Routing\\Router` class would look like::
287320

288321
$locator = new FileLocator(array(__DIR__));
@@ -298,6 +331,6 @@ automatically in the background if you want to use it. A basic example of the
298331

299332
.. note::
300333

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
303336
permissions for that location.

0 commit comments

Comments
 (0)