Skip to content

Commit 0896663

Browse files
committed
Documented the name prefix feature for YAML, XML and PHP files
1 parent 2c9eb89 commit 0896663

File tree

1 file changed

+64
-19
lines changed

1 file changed

+64
-19
lines changed

routing/external_resources.rst

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,35 +136,80 @@ be prefixed with the string ``/site``.
136136
Prefixing the Names of Imported Routes
137137
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138138

139-
You also have the possibility to prefix all route names defined in a controller
140-
class with the ``name`` attribute of the ``@Route`` annotation::
139+
You also have the possibility to prefix the names of all the routes defined in
140+
a controller class or imported from a configuration file:
141141

142-
use Symfony\Component\Routing\Annotation\Route;
142+
.. configuration-block::
143143

144-
/**
145-
* @Route(name="blog_")
146-
*/
147-
class BlogController extends Controller
148-
{
149-
/**
150-
* @Route("/blog", name="index")
151-
*/
152-
public function indexAction()
153-
{
154-
// ...
155-
}
144+
.. code-block:: php-annotations
145+
146+
use Symfony\Component\Routing\Annotation\Route;
156147
157148
/**
158-
* @Route("/blog/posts/{slug}", name="post")
149+
* @Route(name="blog_")
159150
*/
160-
public function showAction(Post $post)
151+
class BlogController extends Controller
161152
{
162-
// ...
153+
/**
154+
* @Route("/blog", name="index")
155+
*/
156+
public function indexAction()
157+
{
158+
// ...
159+
}
160+
161+
/**
162+
* @Route("/blog/posts/{slug}", name="post")
163+
*/
164+
public function showAction(Post $post)
165+
{
166+
// ...
167+
}
163168
}
164-
}
169+
170+
.. code-block:: yaml
171+
172+
# config/routes.yaml
173+
controllers:
174+
resource: '../src/Controller/'
175+
type: annotation
176+
name_prefix: 'blog_'
177+
178+
.. code-block:: xml
179+
180+
<!-- config/routes.xml -->
181+
<?xml version="1.0" encoding="UTF-8" ?>
182+
<routes xmlns="http://symfony.com/schema/routing"
183+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
184+
xsi:schemaLocation="http://symfony.com/schema/routing
185+
http://symfony.com/schema/routing/routing-1.0.xsd">
186+
187+
<import
188+
resource="../src/Controller/"
189+
type="annotation"
190+
name-prefix="blog_" />
191+
</routes>
192+
193+
.. code-block:: php
194+
195+
// config/routes.php
196+
use Symfony\Component\Routing\RouteCollection;
197+
198+
$app = $loader->import('../src/Controller/', 'annotation');
199+
$app->addNamePrefix('blog_');
200+
201+
$collection = new RouteCollection();
202+
$collection->addCollection($app);
203+
204+
return $collection;
165205
166206
In this example, the names of the routes will be ``blog_index`` and ``blog_post``.
167207

208+
.. versionadded:: 4.1
209+
The option to prefix route names in YAML, XML and PHP files was introduced
210+
in Symfony 4.1. Previously only the ``@Route()`` annotation supported this
211+
feature.
212+
168213
Adding a Host Requirement to Imported Routes
169214
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170215

0 commit comments

Comments
 (0)