Skip to content

Commit 84777b2

Browse files
committed
Documented how to load routes from objects/services
1 parent 89bcd82 commit 84777b2

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

routing/custom_route_loader.rst

+51
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,57 @@ containing :class:`Symfony\\Component\\Routing\\Route` objects.
5959
when they are defined in one of the default formats (e.g. XML, YML,
6060
PHP file).
6161

62+
Loading Routes with a Custom Service
63+
------------------------------------
64+
65+
.. versionadded:: 2.8
66+
The option to load routes using Symfony services was introduced in Symfony 2.8.
67+
68+
Using a regular Symfony service is the simplest way to load routes in a
69+
customized way. It's much easier than creating a full custom route loader, so
70+
you should always consider this option first.
71+
72+
To do so, define ``type: service`` as the type of the loaded routing resource
73+
and configure the service and method to call:
74+
75+
.. configuration-block::
76+
77+
.. code-block:: yaml
78+
79+
# app/config/routing.yml
80+
admin_routes:
81+
resource: 'admin_route_loader:loadRoutes'
82+
type: service
83+
84+
.. code-block:: xml
85+
86+
<!-- app/config/routing.xml -->
87+
<?xml version="1.0" encoding="UTF-8" ?>
88+
<routes xmlns="http://symfony.com/schema/routing"
89+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
90+
xsi:schemaLocation="http://symfony.com/schema/routing
91+
http://symfony.com/schema/routing/routing-1.0.xsd">
92+
93+
<import resource="admin_route_loader:loadRoutes" type="service"/>
94+
</routes>
95+
96+
.. code-block:: php
97+
98+
// app/config/routing.php
99+
use Symfony\Component\Routing\RouteCollection;
100+
101+
$collection = new RouteCollection();
102+
$collection->addCollection(
103+
$loader->import("admin_route_loader:loadRoutes", "service")
104+
);
105+
106+
return $collection;
107+
108+
In this example, the routes are loaded by calling the ``loadRoutes()`` method of
109+
the service whose ID is ``admin_route_loader``. Your service doesn't have to
110+
extend or implement any special class, but the called method must return a
111+
:class:`Symfony\\Component\\Routing\\RouteCollection` object.
112+
62113
Creating a custom Loader
63114
------------------------
64115

0 commit comments

Comments
 (0)