Symfony 4 was released on November 30th.
Update now to the best Symfony ever!

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 Use Service Container Parameters in your Routes

How to Use Service Container Parameters in your Routes

Sometimes you may find it useful to make some parts of your routes globally configurable. For instance, if you build an internationalized site, you'll probably start with one or two locales. Surely you'll add a requirement to your routes to prevent a user from matching a locale other than the locales you support.

You could hardcode your _locale requirement in all your routes, but a better solution is to use a configurable service container parameter right inside your routing configuration:

  • YAML
    1
    2
    3
    4
    5
    6
    # config/routes.yaml
    contact:
        path:       /{_locale}/contact
        controller: App\Controller\MainController::contact
        requirements:
            _locale: '%app.locales%'
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <!-- 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="contact" path="/{_locale}/contact">
            <default key="_controller">App\Controller\MainController::contact</default>
            <requirement key="_locale">%app.locales%</requirement>
        </route>
    </routes>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    // config/routes.php
    use Symfony\Component\Routing\RouteCollection;
    use Symfony\Component\Routing\Route;
    
    $collection = new RouteCollection();
    $collection->add('contact', new Route('/{_locale}/contact', array(
        '_controller' => 'App\Controller\MainController::contact',
    ), array(
        '_locale' => '%app.locales%',
    )));
    
    return $collection;
    

You can now control and set the app.locales parameter somewhere in your container:

  • YAML
    1
    2
    3
    # config/services.yaml
    parameters:
        app.locales: en|es
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <!-- config/services.xml -->
    <?xml version="1.0" charset="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <parameters>
            <parameter key="app.locales">en|es</parameter>
        </parameters>
    </container>
    
  • PHP
    1
    2
    // config/services.php
    $container->setParameter('app.locales', 'en|es');
    

You can also use a parameter to define your route path (or part of your path):

  • YAML
    1
    2
    3
    4
    # config/routes.yaml
    some_route:
        path:       /%app.route_prefix%/contact
        controller: App\Controller\MainController::contact
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <!-- 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="some_route" path="/%app.route_prefix%/contact">
            <default key="_controller">App\Controller\MainController::contact</default>
        </route>
    </routes>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    // config/routes.php
    use Symfony\Component\Routing\RouteCollection;
    use Symfony\Component\Routing\Route;
    
    $collection = new RouteCollection();
    $collection->add('some_route', new Route('/%app.route_prefix%/contact', array(
        '_controller' => 'App\Controller\MainController::contact',
    )));
    
    return $collection;
    

Note

Just like in normal service container configuration files, if you actually need a % in your route, you can escape the percent sign by doubling it, e.g. /score-50%%, which would resolve to /score-50%.

However, as the % characters included in any URL are automatically encoded, the resulting URL of this example would be /score-50%25 (%25 is the result of encoding the % character).

For parameter handling within a Dependency Injection Class see Using Parameters within a Dependency Injection Class.

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