Skip to content

Commit 1f8acec

Browse files
committed
More fixes suggested by Ryan
1 parent a1d7599 commit 1f8acec

File tree

1 file changed

+59
-88
lines changed

1 file changed

+59
-88
lines changed

routing.rst

Lines changed: 59 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
Routing
55
=======
66

7-
When your application receives an HTTP request, it executes a
8-
:doc:`controller action </controller>` to generate the HTTP response. The
9-
routing configuration defines which action to run for each incoming URL. It also
10-
provides other useful features, like generating SEO-friendly URLs
11-
(e.g. ``/read/intro-to-symfony`` instead of ``index.php?article_id=57``).
7+
When your application receives a request, it executes a
8+
:doc:`controller action </controller>` to generate the response. The routing
9+
configuration defines which action to run for each incoming URL. It also
10+
provides other useful features, like generating SEO-friendly URLs (e.g.
11+
``/read/intro-to-symfony`` instead of ``index.php?article_id=57``).
1212

1313
Creating Routes
1414
---------------
@@ -332,47 +332,41 @@ with a locale. This can be done by defining a different prefix for each locale
332332
Route Groups
333333
~~~~~~~~~~~~
334334

335-
It's common for a group of routes to share some options (e.g. all routes
336-
related to the blog start with ``/blog``; all routes validate the locale with
337-
the same regular expression; etc.) That's why Symfony includes a feature to
338-
share route configuration.
335+
It's common for a group of routes to share some options (e.g. all routes related
336+
to the blog start with ``/blog``) That's why Symfony includes a feature to share
337+
route configuration.
339338

340-
When defining routes as annotations, add an ``@Route`` annotation to the
341-
controller class to apply options to all the routes defined in that class::
339+
When defining routes as annotations, put the common configuration in the
340+
``@Route`` annotation of the controller class. In other routing formats, define
341+
the common configuration using options when importing the routes.
342342

343-
use Symfony\Component\Routing\Annotation\Route;
343+
.. configuration-block::
344344

345-
/**
346-
* @Route("/blog", requirements={"locale": "en|es|fr"}, name="blog_")
347-
*/
348-
class BlogController
349-
{
350-
/**
351-
* @Route("/{_locale}", name="index")
352-
*/
353-
public function index()
354-
{
355-
// ...
356-
}
345+
.. code-block:: php-annotations
346+
347+
use Symfony\Component\Routing\Annotation\Route;
357348
358349
/**
359-
* @Route("/{_locale}/posts/{slug}", name="post")
350+
* @Route("/blog", requirements={"locale": "en|es|fr"}, name="blog_")
360351
*/
361-
public function show(Post $post)
352+
class BlogController
362353
{
363-
// ...
364-
}
365-
}
366-
367-
In this example, the route of the ``index()`` action will be called ``blog_index``
368-
and its URL will be ``/blog/``. The route of the ``show()`` action will be called
369-
``blog_post`` and its URL will be ``/blog/{_locale}/posts/{slug}``. Both routes
370-
will also validate that the ``_locale`` parameter matches the regular expression
371-
defined in the class annotation.
372-
373-
The same can be used when importing routes from configuration files:
354+
/**
355+
* @Route("/{_locale}", name="index")
356+
*/
357+
public function index()
358+
{
359+
// ...
360+
}
374361
375-
.. configuration-block::
362+
/**
363+
* @Route("/{_locale}/posts/{slug}", name="post")
364+
*/
365+
public function show(Post $post)
366+
{
367+
// ...
368+
}
369+
}
376370
377371
.. code-block:: yaml
378372
@@ -387,6 +381,9 @@ The same can be used when importing routes from configuration files:
387381
# these requirements are added to all imported routes
388382
requirements:
389383
locale: 'en|es|fr'
384+
# An imported route with an empty URL will become "/blog/"
385+
# Uncomment this option to make that URL "/blog" instead
386+
# trailing_slash_on_root: false
390387
391388
.. code-block:: xml
392389
@@ -397,14 +394,25 @@ The same can be used when importing routes from configuration files:
397394
xsi:schemaLocation="http://symfony.com/schema/routing
398395
https://symfony.com/schema/routing/routing-1.0.xsd">
399396
400-
<import resource="../src/Controller/" type="annotation">
401-
<!-- this is added to the beginning of all imported route URLs -->
402-
<prefix>/blog</prefix>
403-
<!-- this is added to the beginning of all imported route names -->
404-
<name-prefix>blog_</name-prefix>
397+
<!--
398+
the 'prefix' value is added to the beginning of all imported route URLs
399+
the 'name-prefix' value is added to the beginning of all imported route names
400+
-->
401+
<import resource="../src/Controller/"
402+
type="annotation"
403+
prefix="/blog"
404+
name-prefix="blog_">
405405
<!-- these requirements are added to all imported routes -->
406406
<requirement key="locale">en|es|fr</requirement>
407407
</import>
408+
409+
<!-- An imported route with an empty URL will become "/blog/"
410+
Uncomment this option to make that URL "/blog" instead -->
411+
<import resource="../src/Controller/" type="annotation"
412+
prefix="/blog"
413+
trailing-slash-on-root="false">
414+
<!-- ... -->
415+
</import>
408416
</routes>
409417
410418
.. code-block:: php
@@ -416,58 +424,21 @@ The same can be used when importing routes from configuration files:
416424
$routes->import('../src/Controller/', 'annotation')
417425
// this is added to the beginning of all imported route URLs
418426
->prefix('/blog')
427+
// An imported route with an empty URL will become "/blog/"
428+
// Pass FALSE as the second argument to make that URL "/blog" instead
429+
// ->prefix('/blog', false)
419430
// this is added to the beginning of all imported route names
420431
->namePrefix('blog_')
421432
// these requirements are added to all imported routes
422433
->requirements(['locale' => 'en|es|fr'])
423434
;
424435
};
425436
426-
.. note::
427-
428-
If any of the prefixed routes defines an empty path, Symfony adds a trailing
429-
slash to it. In the previous example, an empty path prefixed with ``/site``
430-
will result in the ``/site/`` URL. If you want to avoid this behavior, set
431-
the ``trailing_slash_on_root`` option to ``false``:
432-
433-
.. configuration-block::
434-
435-
.. code-block:: yaml
436-
437-
# config/routes.yaml
438-
controllers:
439-
resource: '../src/Controller/'
440-
type: annotation
441-
prefix: /blog
442-
trailing_slash_on_root: false
443-
444-
.. code-block:: xml
445-
446-
<!-- config/routes.xml -->
447-
<?xml version="1.0" encoding="UTF-8" ?>
448-
<routes xmlns="http://symfony.com/schema/routing"
449-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
450-
xsi:schemaLocation="http://symfony.com/schema/routing
451-
https://symfony.com/schema/routing/routing-1.0.xsd">
452-
453-
<import resource="../src/Controller/"
454-
type="annotation"
455-
prefix="/blog"
456-
trailing-slash-on-root="false"/>
457-
</routes>
458-
459-
.. code-block:: php
460-
461-
// config/routes.php
462-
use App\Controller\ArticleController;
463-
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
464-
465-
return function (RoutingConfigurator $routes) {
466-
$routes->import('../src/Controller/', 'annotation')
467-
// the second argument is a boolean to configure 'trailing_slash_on_root'
468-
->prefix('/blog', false)
469-
;
470-
};
437+
In this example, the route of the ``index()`` action will be called ``blog_index``
438+
and its URL will be ``/blog/``. The route of the ``show()`` action will be called
439+
``blog_post`` and its URL will be ``/blog/{_locale}/posts/{slug}``. Both routes
440+
will also validate that the ``_locale`` parameter matches the regular expression
441+
defined in the class annotation.
471442

472443
.. seealso::
473444

0 commit comments

Comments
 (0)