Skip to content

Commit 0f3c049

Browse files
committed
[symfony#2339] Proofreading the nice new custom route loader cookbook article
1 parent e804e02 commit 0f3c049

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

cookbook/routing/custom_route_loader.rst

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
.. index::
22
single: Routing; Custom route loader
33

4-
How to Create a Custom Route Loader
4+
How to create a custom Route Loader
55
===================================
66

7-
A custom route loader allows you to add routes to an application, without
8-
writing them down in for instance a Yaml file. This comes in handy when
9-
you have made a bundle and don't want to manually add the routes for the
10-
bundle to ``app/config/routing.yml``. Especially when you want to make the
11-
bundle reusable, or when you have open-sourced it, this would slow down
12-
the installation process and make it error-prone.
7+
A custom route loader allows you to add routes to an application without
8+
including them, for example, in a Yaml file. This comes in handy when
9+
you have a bundle but don't want to manually add the routes for the bundle
10+
to ``app/config/routing.yml``. This may be especially important when you want
11+
to make the bundle reusable, or when you have open-sourced it as this would
12+
slow down the installation process and make it error-prone.
1313

14-
But you can also use a custom route loader when your routes correspond
15-
to a certain pattern and you don't want to or can't write them all out by
16-
hand. For instance when you have CRUD controllers of which the routes all
17-
correspond to a pattern like ``*_new``, ``*_edit``, etc.
14+
Alternatively, you could also use a custom route loader when you want your
15+
routes to be automatically generated or located based on some convention or
16+
pattern. One example is the `FOSRestBundle`_ where routing is generated based
17+
off the names of the action methods in a controller.
1818

1919
.. note::
2020

@@ -44,23 +44,24 @@ Take these lines from ``routing.yml``:
4444
type: annotation
4545
prefix: /demo
4646
47-
The main loader tries all the delegate loaders and calls their
48-
:method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports`
49-
with the given resource (``@AcmeDemoBundle/Controller/DemoController.php``)
50-
and type ("annotation") as arguments. When one of the loader returns ``true``,
51-
its method :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load`
52-
will be called, and the loader returns a :class:`Symfony\\Component\\Routing\\RouteCollection`
47+
When the main loader parses this, it tries all the delegate loaders and calls
48+
their :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports`
49+
method with the given resource (``@AcmeDemoBundle/Controller/DemoController.php``)
50+
and type (``annotation``) as arguments. When one of the loader returns ``true``,
51+
its :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load` method
52+
will be called, which should return a :class:`Symfony\\Component\\Routing\\RouteCollection`
5353
containing :class:`Symfony\\Component\\Routing\\Route` objects.
5454

5555
Creating a Custom Loader
5656
------------------------
5757

58-
To load routes in another way than using annotations, Yaml or XML files,
59-
you need to create a custom route loader. This loader should implement
60-
:class:`Symfony\\Component\\Config\\Loader\\LoaderInterface`.
58+
To load routes from some custom source (i.e. from something other than annotations,
59+
Yaml or XML files), you need to create a custom route loader. This loader
60+
should implement :class:`Symfony\\Component\\Config\\Loader\\LoaderInterface`.
6161

62-
The sample loader below supports resources of type "extra". The resource
63-
name itself is not used in the example::
62+
The sample loader below supports loading routing resources with a type of
63+
``extra``. The type ``extra`` isn't important - you can just invent any resource
64+
type you want. The resource name itself is not actually used in the example::
6465

6566
namespace Acme\DemoBundle\Routing;
6667

@@ -105,12 +106,13 @@ name itself is not used in the example::
105106

106107
public function getResolver()
107108
{
108-
// will be explained later
109+
// needed, but can be blank, unless you want to load other resources
110+
// and if you do, using the Loader base class is easier (see below)
109111
}
110112

111113
public function setResolver(LoaderResolver $resolver)
112114
{
113-
// same here
115+
// same as above
114116
}
115117
}
116118

@@ -160,12 +162,17 @@ Notice the tag ``routing.loader``. All services with this tag will be marked
160162
as potential route loaders and added as specialized routers to the
161163
:class:`Symfony\\Bundle\\FrameworkBundle\\Routing\\DelegatingLoader`.
162164

163-
Finally, we only need to add a few extra lines to the routing configuration:
165+
Using the Custom Loader
166+
~~~~~~~~~~~~~~~~~~~~~~~
167+
168+
If you did nothing else, your custom routing loader would *not* be called.
169+
Instead, you only need to add a few extra lines to the routing configuration:
164170

165171
.. configuration-block::
166172

167173
.. code-block:: yaml
168174
175+
# app/config/routing.yml
169176
AcmeDemoBundle_Extra:
170177
resource: .
171178
type: extra
@@ -182,6 +189,7 @@ Finally, we only need to add a few extra lines to the routing configuration:
182189
183190
.. code-block:: php
184191
192+
// app/config/routing.php
185193
use Symfony\Component\Routing\RouteCollection;
186194
187195
$collection = new RouteCollection();
@@ -197,9 +205,8 @@ for the ``ExtraLoader``, so we set it to ".".
197205
.. note::
198206

199207
The routes defined using custom route loaders will be automatically
200-
cached by the framework. So whenever you change something to the behavior
201-
of the loader, don't forget to clear the cache.
202-
208+
cached by the framework. So whenever you change something in the loader
209+
class itself, don't forget to clear the cache.
203210

204211
More Advanced Loaders
205212
---------------------
@@ -213,8 +220,8 @@ to load secondary routing resources.
213220
Of course you still need to implement
214221
:method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports`
215222
and :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load`.
216-
Whenever you want to load another resource, for instance a Yaml routing
217-
configuration file, you can call the
223+
Whenever you want to load another resource - for instance a Yaml routing
224+
configuration file - you can call the
218225
:method:`Symfony\\Component\\Config\\Loader\\Loader::import` method::
219226

220227
namespace Acme\DemoBundle\Routing;

reference/dic_tags.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ of your configuration, and tag it with ``routing.loader``:
462462
->addTag('routing.loader')
463463
;
464464
465+
For more information, see :doc:`/cookbook/routing/custom_route_loader`.
466+
465467
security.listener.factory
466468
-------------------------
467469

0 commit comments

Comments
 (0)