Using Parameters in Routes

You can use parameters from parameters.yml in routes. There are serveral use-cases when this may be useful.

Let’s say you want to localize your routes but you want to store the list of available locales only once. Then you could define a parameter in config.yml:

parameters:
    allowed_locales: en|de|fr

Now you can use the parameter in your routing.yml:

some_bundle:
    resource: "@MyBundle/Controller/"
    prefix:   /{_locale}
    type:     annotation
    requirements:
        _locale: %allowed_locales%

Another use-case is routes based on a hostname. Instead of writing the hostname directly into routing.yml you could use a parameter, as seen in the official documentation.

As fas as I know parameters only work for routes defined in YAML, XML or PHP. If it also works for annotation routes, please let me know.

Importing Routes from a Controller

This is how you import routes from a specific controller (only works if you used annotations):

whatever_routes:
    resource: "@MyBundle/Controller/WhateverController.php"
    type:     annotation

This is very useful if you have multiple controllers in single bundle but you can’t import the whole Controller directory at once because some routes have to be treated in a special way, e.g. you want a different prefix for every controller.

whatever_routes:
    resource: "@MyBundle/Controller/WhateverController.php"
    prefix:   /some-bundle/whatever
    type:     annotation

other_routes:
    resource: "@MyBundle/Controller/AnotherController.php"
    prefix:   /some-bundle/another
    type:     annotation

For reference, this is the original documentation of that feature.