Skip to content

Commit 78e055a

Browse files
committed
Another proposal about the components docs
1 parent 7bd3697 commit 78e055a

File tree

2 files changed

+210
-308
lines changed

2 files changed

+210
-308
lines changed

components/routing.rst

Lines changed: 2 additions & 308 deletions
Original file line numberDiff line numberDiff line change
@@ -21,311 +21,5 @@ Installation
2121
Usage
2222
-----
2323

24-
The main :doc:`Symfony routing` article explains all the features of this
25-
component when used inside a Symfony application. This article only explains
26-
the things you need to do to use it in a non-Symfony PHP application.
27-
28-
Routing System Setup
29-
--------------------
30-
31-
A routing system has three parts:
32-
33-
* A :class:`Symfony\\Component\\Routing\\RouteCollection`, which contains the
34-
route definitions (instances of the class :class:`Symfony\\Component\\Routing\\Route`);
35-
* A :class:`Symfony\\Component\\Routing\\RequestContext`, which has information
36-
about the request;
37-
* A :class:`Symfony\\Component\\Routing\\Matcher\\UrlMatcher`, which performs
38-
the mapping of the path to a single route.
39-
40-
Here is a quick example::
41-
42-
use App\Controller\BlogController;
43-
use Symfony\Component\Routing\Matcher\UrlMatcher;
44-
use Symfony\Component\Routing\RequestContext;
45-
use Symfony\Component\Routing\Route;
46-
use Symfony\Component\Routing\RouteCollection;
47-
48-
$route = new Route('/blog/{slug}', ['_controller' => BlogController::class])
49-
$routes = new RouteCollection();
50-
$routes->add('blog_show', $route);
51-
52-
$context = new RequestContext('/');
53-
54-
// Routing can match routes with incoming requests
55-
$matcher = new UrlMatcher($routes, $context);
56-
$parameters = $matcher->match('/blog/lorem-ipsum');
57-
// $parameters = [
58-
// '_controller' => 'App\Controller\BlogController',
59-
// 'slug' => 'lorem-ipsum',
60-
// '_route' => 'blog_show'
61-
// ]
62-
63-
// Routing can also generate URLs for a given route
64-
$generator = new UrlGenerator($routes, $context);
65-
$url = $generator->generate('blog_show', [
66-
'slug' => 'my-blog-post',
67-
]);
68-
// $url = '/blog/my-blog-post'
69-
70-
The :method:`RouteCollection::add() <Symfony\\Component\\Routing\\RouteCollection::add>`
71-
method takes two arguments. The first is the name of the route. The second
72-
is a :class:`Symfony\\Component\\Routing\\Route` object, which expects a
73-
URL path and some array of custom variables in its constructor. This array
74-
of custom variables can be *anything* that's significant to your application,
75-
and is returned when that route is matched.
76-
77-
The :method:`UrlMatcher::match() <Symfony\\Component\\Routing\\Matcher\\UrlMatcher::match>`
78-
returns the variables you set on the route as well as the route parameters.
79-
Your application can now use this information to continue processing the request.
80-
In addition to the configured variables, a ``_route`` key is added, which holds
81-
the name of the matched route.
82-
83-
If no matching route can be found, a
84-
:class:`Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException` will
85-
be thrown.
86-
87-
Defining Routes
88-
---------------
89-
90-
A full route definition can contain up to eight parts::
91-
92-
$route = new Route(
93-
'/archive/{month}', // path
94-
['_controller' => 'showArchive'], // default values
95-
['month' => '[0-9]{4}-[0-9]{2}', 'subdomain' => 'www|m'], // requirements
96-
[], // options
97-
'{subdomain}.example.com', // host
98-
[], // schemes
99-
[], // methods
100-
'context.getHost() matches "/(secure|admin).example.com/"' // condition
101-
);
102-
103-
// ...
104-
105-
$parameters = $matcher->match('/archive/2012-01');
106-
// [
107-
// '_controller' => 'showArchive',
108-
// 'month' => '2012-01',
109-
// 'subdomain' => 'www',
110-
// '_route' => ...
111-
// ]
112-
113-
$parameters = $matcher->match('/archive/foo');
114-
// throws ResourceNotFoundException
115-
116-
Route Collections
117-
-----------------
118-
119-
You can add routes or other instances of
120-
:class:`Symfony\\Component\\Routing\\RouteCollection` to *another* collection.
121-
This way you can build a tree of routes. Additionally you can define common
122-
options for all routes of a subtree using methods provided by the
123-
``RouteCollection`` class::
124-
125-
$rootCollection = new RouteCollection();
126-
127-
$subCollection = new RouteCollection();
128-
$subCollection->add(...);
129-
$subCollection->add(...);
130-
$subCollection->addPrefix('/prefix');
131-
$subCollection->addDefaults([...]);
132-
$subCollection->addRequirements([...]);
133-
$subCollection->addOptions([...]);
134-
$subCollection->setHost('{subdomain}.example.com');
135-
$subCollection->setMethods(['POST']);
136-
$subCollection->setSchemes(['https']);
137-
$subCollection->setCondition('context.getHost() matches "/(secure|admin).example.com/"');
138-
139-
$rootCollection->addCollection($subCollection);
140-
141-
Setting the Request Parameters
142-
------------------------------
143-
144-
The :class:`Symfony\\Component\\Routing\\RequestContext` provides information
145-
about the current request. You can define all parameters of an HTTP request
146-
with this class via its constructor::
147-
148-
public function __construct(
149-
$baseUrl = '',
150-
$method = 'GET',
151-
$host = 'localhost',
152-
$scheme = 'http',
153-
$httpPort = 80,
154-
$httpsPort = 443,
155-
$path = '/',
156-
$queryString = ''
157-
)
158-
159-
.. _components-routing-http-foundation:
160-
161-
Normally you can pass the values from the ``$_SERVER`` variable to populate the
162-
:class:`Symfony\\Component\\Routing\\RequestContext`. But if you use the
163-
:doc:`HttpFoundation </components/http_foundation>` component, you can use its
164-
:class:`Symfony\\Component\\HttpFoundation\\Request` class to feed the
165-
:class:`Symfony\\Component\\Routing\\RequestContext` in a shortcut::
166-
167-
use Symfony\Component\HttpFoundation\Request;
168-
169-
$context = new RequestContext();
170-
$context->fromRequest(Request::createFromGlobals());
171-
172-
Checking if a Route Exists
173-
--------------------------
174-
175-
In highly dynamic applications, it may be necessary to check whether a route
176-
exists before using it to generate a URL. In those cases, don't use the
177-
:method:`Symfony\\Component\\Routing\\Router::getRouteCollection` method because
178-
that regenerates the routing cache and slows down the application.
179-
180-
Instead, try to generate the URL and catch the
181-
:class:`Symfony\\Component\\Routing\\Exception\\RouteNotFoundException` thrown
182-
when the route doesn't exist::
183-
184-
use Symfony\Component\Routing\Exception\RouteNotFoundException;
185-
186-
// ...
187-
188-
try {
189-
$url = $generator->generate($dynamicRouteName, $parameters);
190-
} catch (RouteNotFoundException $e) {
191-
// the route is not defined...
192-
}
193-
194-
Loading Routes
195-
--------------
196-
197-
The Routing component comes with a number of loader classes, each giving you the
198-
ability to load a collection of route definitions from external resources.
199-
200-
File Routing Loaders
201-
~~~~~~~~~~~~~~~~~~~~
202-
203-
Each loader expects a :class:`Symfony\\Component\\Config\\FileLocator` instance
204-
as the constructor argument. You can use the :class:`Symfony\\Component\\Config\\FileLocator`
205-
to define an array of paths in which the loader will look for the requested files.
206-
If the file is found, the loader returns a :class:`Symfony\\Component\\Routing\\RouteCollection`.
207-
208-
If you're using the ``YamlFileLoader``, then route definitions look like this:
209-
210-
.. code-block:: yaml
211-
212-
# routes.yaml
213-
route1:
214-
path: /foo
215-
controller: MyController::fooAction
216-
methods: GET|HEAD
217-
218-
route2:
219-
path: /foo/bar
220-
controller: FooBarInvokableController
221-
methods: PUT
222-
223-
To load this file, you can use the following code. This assumes that your
224-
``routes.yaml`` file is in the same directory as the below code::
225-
226-
use Symfony\Component\Config\FileLocator;
227-
use Symfony\Component\Routing\Loader\YamlFileLoader;
228-
229-
// looks inside *this* directory
230-
$fileLocator = new FileLocator([__DIR__]);
231-
$loader = new YamlFileLoader($fileLocator);
232-
$routes = $loader->load('routes.yaml');
233-
234-
Besides :class:`Symfony\\Component\\Routing\\Loader\\YamlFileLoader` there are two
235-
other loaders that work the same way:
236-
237-
* :class:`Symfony\\Component\\Routing\\Loader\\XmlFileLoader`
238-
* :class:`Symfony\\Component\\Routing\\Loader\\PhpFileLoader`
239-
240-
If you use the :class:`Symfony\\Component\\Routing\\Loader\\PhpFileLoader` you
241-
have to provide the name of a PHP file which returns a callable handling a
242-
:class:`Symfony\\Component\\Routing\\Loader\\Configurator\\RoutingConfigurator`.
243-
This class allows to chain imports, collections or simple route definition calls::
244-
245-
// RouteProvider.php
246-
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
247-
248-
return function (RoutingConfigurator $routes) {
249-
$routes->add('route_name', '/foo')
250-
->controller('ExampleController')
251-
// ...
252-
;
253-
};
254-
255-
Closure Routing Loaders
256-
~~~~~~~~~~~~~~~~~~~~~~~
257-
258-
There is also the :class:`Symfony\\Component\\Routing\\Loader\\ClosureLoader`, which
259-
calls a closure and uses the result as a :class:`Symfony\\Component\\Routing\\RouteCollection`::
260-
261-
use Symfony\Component\Routing\Loader\ClosureLoader;
262-
263-
$closure = function () {
264-
return new RouteCollection();
265-
};
266-
267-
$loader = new ClosureLoader();
268-
$routes = $loader->load($closure);
269-
270-
Annotation Routing Loaders
271-
~~~~~~~~~~~~~~~~~~~~~~~~~~
272-
273-
Last but not least there are
274-
:class:`Symfony\\Component\\Routing\\Loader\\AnnotationDirectoryLoader` and
275-
:class:`Symfony\\Component\\Routing\\Loader\\AnnotationFileLoader` to load
276-
route definitions from class annotations. The specific details are left
277-
out here.
278-
279-
.. include:: /_includes/_annotation_loader_tip.rst.inc
280-
281-
The all-in-one Router
282-
~~~~~~~~~~~~~~~~~~~~~
283-
284-
The :class:`Symfony\\Component\\Routing\\Router` class is an all-in-one package
285-
to use the Routing component. The constructor expects a loader instance,
286-
a path to the main route definition and some other settings::
287-
288-
public function __construct(
289-
LoaderInterface $loader,
290-
$resource,
291-
array $options = [],
292-
RequestContext $context = null,
293-
LoggerInterface $logger = null
294-
);
295-
296-
With the ``cache_dir`` option you can enable route caching (if you provide a
297-
path) or disable caching (if it's set to ``null``). The caching is done
298-
automatically in the background if you want to use it. A basic example of the
299-
:class:`Symfony\\Component\\Routing\\Router` class would look like::
300-
301-
$fileLocator = new FileLocator([__DIR__]);
302-
$requestContext = new RequestContext('/');
303-
304-
$router = new Router(
305-
new YamlFileLoader($fileLocator),
306-
'routes.yaml',
307-
['cache_dir' => __DIR__.'/cache'],
308-
$requestContext
309-
);
310-
$parameters = $router->match('/foo/bar');
311-
$url = $router->generate('some_route', ['parameter' => 'value']);
312-
313-
.. note::
314-
315-
If you use caching, the Routing component will compile new classes which
316-
are saved in the ``cache_dir``. This means your script must have write
317-
permissions for that location.
318-
319-
Learn more
320-
----------
321-
322-
.. toctree::
323-
:maxdepth: 1
324-
:glob:
325-
326-
/routing
327-
/routing/*
328-
/controller
329-
/controller/*
330-
331-
.. _Packagist: https://packagist.org/packages/symfony/routing
24+
The documentation about using this component has been moved to the
25+
main :doc:`Symfony routing` article.

0 commit comments

Comments
 (0)