You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.

How to Define Route Requirements

4.2 version
Maintained Unmaintained

How to Define Route Requirements

Route requirements can be used to make a specific route only match under specific conditions. The simplest example involves restricting a routing {wildcard} to only match some regular expression:

  • Annotations
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    // src/Controller/BlogController.php
    namespace App\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\Routing\Annotation\Route;
    
    class BlogController extends AbstractController
    {
        /**
         * @Route("/blog/{page}", name="blog_list", requirements={"page"="\d+"})
         */
        public function list($page)
        {
            // ...
        }
    }
    
  • YAML
    1
    2
    3
    4
    5
    6
    # config/routes.yaml
    blog_list:
        path:      /blog/{page}
        controller: App\Controller\BlogController::list
        requirements:
            page: '\d+'
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <!-- config/routes.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing
            http://symfony.com/schema/routing/routing-1.0.xsd">
    
        <route id="blog_list" path="/blog/{page}">
            <default key="_controller">App\Controller\BlogController::list</default>
            <requirement key="page">\d+</requirement>
        </route>
    
        <!-- ... -->
    </routes>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    // config/routes.php
    use Symfony\Component\Routing\RouteCollection;
    use Symfony\Component\Routing\Route;
    
    $routes = new RouteCollection();
    $routes->add('blog_list', new Route('/blog/{page}', array(
        '_controller' => 'App\Controller\BlogController::list',
    ), array(
        'page' => '\d+',
    )));
    
    // ...
    
    return $routes;
    

Thanks to the \d+ requirement (i.e. a "digit" of any length), /blog/2 will match this route but /blog/some-string will not match.

Why would you ever care about requirements? If a request matches two routes, then the first route always wins. By adding requirements to the first route, you can make each route match in just the right situations. See Adding {wildcard} Requirements for an example.

Since the parameter requirements are regular expressions, the complexity and flexibility of each requirement is entirely up to you. Suppose the homepage of your application is available in two different languages, based on the URL:

  • Annotations
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    // src/Controller/MainController.php
    
    // ...
    class MainController extends AbstractController
    {
        /**
         * @Route("/{_locale}", defaults={"_locale"="en"}, requirements={
         *     "_locale"="en|fr"
         * })
         */
        public function homepage($_locale)
        {
        }
    }
    
  • YAML
    1
    2
    3
    4
    5
    6
    7
    # config/routes.yaml
    homepage:
        path:       /{_locale}
        controller: App\Controller\MainController::homepage
        defaults:   { _locale: en }
        requirements:
            _locale:  en|fr
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    <!-- config/routes.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing
            http://symfony.com/schema/routing/routing-1.0.xsd">
    
        <route id="homepage" path="/{_locale}">
            <default key="_controller">App\Controller\MainController::homepage</default>
            <default key="_locale">en</default>
            <requirement key="_locale">en|fr</requirement>
        </route>
    </routes>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    // config/routes.php
    use Symfony\Component\Routing\RouteCollection;
    use Symfony\Component\Routing\Route;
    
    $routes = new RouteCollection();
    $routes->add('homepage', new Route('/{_locale}', array(
        '_controller' => 'App\Controller\MainController::homepage',
        '_locale'     => 'en',
    ), array(
        '_locale' => 'en|fr',
    )));
    
    return $routes;
    

For incoming requests, the {_locale} portion of the URL is matched against the regular expression (en|fr).

Path Parameters
/ {_locale} = "en"
/en {_locale} = "en"
/fr {_locale} = "fr"
/es won't match this route

Note

You can enable UTF-8 route matching by setting the utf8 option when declaring or importing routes. This will make e.g. a . in requirements match any UTF-8 characters instead of just a single byte.

Tip

The route requirements can also include container parameters, as explained in this article. This comes in handy when the regular expression is very complex and used repeatedly in your application.

Adding HTTP Method Requirements

In addition to the URL, you can also match on the method of the incoming request (i.e. GET, HEAD, POST, PUT, DELETE). Suppose you create an API for your blog and you have 2 routes: One for displaying a post (on a GET or HEAD request) and one for updating a post (on a PUT request). This can be accomplished with the following route configuration:

  • Annotations
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    // src/Controller/BlogApiController.php
    namespace App\Controller;
    
    // ...
    
    class BlogApiController extends AbstractController
    {
        /**
         * @Route("/api/posts/{id}", methods={"GET","HEAD"})
         */
        public function show($id)
        {
            // ... return a JSON response with the post
        }
    
        /**
         * @Route("/api/posts/{id}", methods="PUT")
         */
        public function edit($id)
        {
            // ... edit a post
        }
    }
    
  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    # config/routes.yaml
    api_post_show:
        path:       /api/posts/{id}
        controller: App\Controller\BlogApiController::show
        methods:    [GET, HEAD]
    
    api_post_edit:
        path:       /api/posts/{id}
        controller: App\Controller\BlogApiController::edit
        methods:    [PUT]
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    <!-- config/routes.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing
            http://symfony.com/schema/routing/routing-1.0.xsd">
    
        <route id="api_post_show" path="/api/posts/{id}" methods="GET|HEAD">
            <default key="_controller">App\Controller\BlogApiController::show</default>
        </route>
    
        <route id="api_post_edit" path="/api/posts/{id}" methods="PUT">
            <default key="_controller">App\Controller\BlogApiController::edit</default>
        </route>
    </routes>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    // config/routes.php
    use Symfony\Component\Routing\RouteCollection;
    use Symfony\Component\Routing\Route;
    
    $routes = new RouteCollection();
    $routes->add('api_post_show', new Route('/api/posts/{id}', array(
        '_controller' => 'App\Controller\BlogApiController::show',
    ), array(), array(), '', array(), array('GET', 'HEAD')));
    
    $routes->add('api_post_edit', new Route('/api/posts/{id}', array(
        '_controller' => 'App\Controller\BlogApiController::edit',
    ), array(), array(), '', array(), array('PUT')));
    
    return $routes;
    

Despite the fact that these two routes have identical paths (/api/posts/{id}), the first route will match only GET or HEAD requests and the second route will match only PUT requests. This means that you can display and edit the post with the same URL, while using distinct controllers for the two actions.

Note

If no methods are specified, the route will match on all methods.

Tip

If you're using HTML forms and HTTP methods other than GET and POST, you'll need to include a _method parameter to fake the HTTP method. See How to Change the Action and Method of a Form for more information.

Adding a Host Requirement

You can also match on the HTTP host of the incoming request. For more information, see How to Match a Route Based on the Host in the Routing component documentation.

Adding Dynamic Requirements with Expressions

For really complex requirements, you can use dynamic expressions to match any information on the request. See How to Restrict Route Matching through Conditions.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.