Skip to content

Routing Configuration Reference

Edit this page

This reference describes all the options available to define routes with PHP attributes and in the YAML, XML and PHP routing files (e.g. config/routes.yaml).

See also

The options of the router itself (default_uri, strict_requirements, utf8, etc.) are defined under the framework.router key and are explained in the FrameworkBundle configuration reference.

7.4

The XML format to configure routes is deprecated in Symfony 7.4.

Route Options

Each route has a unique name: the name argument of the #[Route] attribute, the key of the route in YAML, the id attribute in XML or the first argument of the add() method in PHP. When a #[Route] attribute doesn't define a name, Symfony generates one from the controller class and method names.

alias

type: string DeprecatedAlias

Defines another name for an existing route. In YAML, XML and PHP files, the alias is a separate route definition whose only options are alias and deprecated. In PHP attributes, the alias argument lists the alternative names of the route. Read more about route aliasing.

1
2
3
4
5
6
// src/Controller/ProductController.php
#[Route('/product/{id}', name: 'product_show', alias: ['product_details'])]
public function show(int $id): Response
{
    // ...
}

condition

type: string default: null

An expression that must return true for the route to match. Conditions are ignored when generating URLs. Read more about the variables and functions available in matching expressions.

1
2
3
4
5
6
// src/Controller/BlogController.php
#[Route('/posts/{id}', name: 'post_show', condition: "params['id'] < 1000")]
public function show(int $id): Response
{
    // ...
}

controller

type: string | array

The controller to execute when the route matches. It's a shortcut for the _controller value of the defaults option. PHP attributes don't define this option because the controller is the method (or the invokable class) where the attribute is applied.

Use the controller_class::method_name syntax, or only controller_class if the controller defines an __invoke() method. In PHP files, you can also use an array such as [BlogController::class, 'show'].

defaults

type: array default: []

The default values of the route parameters. It can also define extra parameters that are not part of the path and the special parameters such as _controller, _format or _locale (use the controller, format and locale options instead).

1
2
3
4
5
6
7
8
9
10
// src/Controller/BlogController.php
#[Route(
    '/blog/{page}',
    name: 'blog_list',
    defaults: ['page' => 1, 'title' => 'Hello world!'],
)]
public function list(int $page, string $title): Response
{
    // ...
}

deprecated

type: array

Marks a route alias as deprecated, so Symfony triggers a deprecation when using it. This option is only allowed in alias definitions and it accepts these keys:

package (required)
The name of the package that deprecated the alias (e.g. acme/package).
version (required)
The version of the package that deprecated the alias (e.g. 1.2).
message
A custom deprecation message. It must include the %alias_id% placeholder, which is replaced by the name of the alias.

In PHP attributes, pass a DeprecatedAlias object to the alias option. Read more about deprecating route aliases.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// src/Controller/ProductController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\DeprecatedAlias;
use Symfony\Component\Routing\Attribute\Route;

class ProductController
{
    // arguments: alias name, package, version and (optionally) message
    #[Route(
        '/product/{id}',
        name: 'product_details',
        alias: new DeprecatedAlias('product_show', 'acme/package', '1.2'),
    )]
    public function show(int $id): Response
    {
        // ...
    }
}

env

type: string | array default: null

Registers the route only in the given configuration environments. This option only exists in PHP attributes. In the other formats, wrap the routes with when@<env> (YAML) or <when env="..."> (XML), or check the value of $routes->env() (PHP).

1
2
3
4
5
6
// src/Controller/DefaultController.php
#[Route('/tools', name: 'tools', env: ['dev', 'test'])]
public function developerTools(): Response
{
    // ...
}

7.4

The ability to pass an array of environments to the env option was introduced in Symfony 7.4.

format

type: string default: null

A shortcut for the _format value of the defaults option. It sets the request format, which is used, for example, to set the Content-Type of the response.

host

type: string | array default: null

The HTTP host that the route must match. It can include route parameters (e.g. {subdomain}.example.com). Pass an array to define a different host per locale (this isn't supported in PHP attributes).

1
2
3
4
5
6
// src/Controller/MainController.php
#[Route('/', name: 'mobile_homepage', host: 'm.example.com')]
public function mobileHomepage(): Response
{
    // ...
}

locale

type: string default: null

A shortcut for the _locale value of the defaults option. It sets the locale of the request.

methods

type: string | array default: []

The HTTP methods that the route responds to. When empty, the route matches any method. Read more about matching HTTP methods.

1
2
3
4
5
6
// src/Controller/BlogApiController.php
#[Route('/api/posts/{id}', name: 'api_post_show', methods: ['GET', 'HEAD'])]
public function show(int $id): Response
{
    // ...
}

options

type: array default: []

Low-level options of the route. The built-in ones are compiler_class (the class that compiles the route, which is RouteCompiler by default) and utf8 (set it with the utf8 option instead).

path

type: string | array required

The URL path that the route matches. It can include route parameters wrapped in curly braces (e.g. /blog/{slug}), with optional inline requirements and default values (e.g. /blog/{page<\d+>?1}). Pass an array to define a different path per locale and create localized routes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// src/Controller/BlogController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class BlogController extends AbstractController
{
    #[Route('/blog/{slug}', name: 'blog_show')]
    public function show(string $slug): Response
    {
        // ...
    }
}

priority

type: integer default: 0

Symfony evaluates routes with a higher priority before routes with a lower priority. This option only exists in PHP attributes. In the other formats, routes are evaluated in the order in which they are defined:

1
2
3
4
5
6
7
8
// src/Controller/BlogController.php
// without the priority, the 'blog_show' route defined before this one
// (with the '/blog/{slug}' path) would match the '/blog/list' URL
#[Route('/blog/list', name: 'blog_list', priority: 2)]
public function list(): Response
{
    // ...
}

requirements

type: array default: []

The PHP regular expressions that route parameters (in the path and in the host) must match. The Requirement enum defines constants for the most common requirements (digits, UUIDs, etc.). Read more about parameters validation.

1
2
3
4
5
6
// src/Controller/BlogController.php
#[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => '\d+'])]
public function list(int $page): Response
{
    // ...
}

schemes

type: string | array default: []

The URL schemes (e.g. https) that the route responds to. When empty, the route matches any scheme. When the current request uses a different scheme, the generated URLs of this route are absolute and use the right scheme. Read more about forcing HTTPS.

stateless

type: boolean default: false

When true, using the session during the request throws an exception (if kernel.debug is enabled) or logs a warning (otherwise). Read more about stateless routes.

utf8

type: boolean default: framework.router.utf8

A shortcut for the utf8 value of the options option. When true, route parameters and requirements match UTF-8 characters instead of single bytes.

1
2
3
4
5
6
// src/Controller/ProductController.php
#[Route('/products/{name}', name: 'product_show', utf8: true)]
public function show(string $name): Response
{
    // ...
}

Route Import Options

Imports load the routes defined in another resource (a file, a directory of controllers with PHP attributes, etc.) and apply some common configuration to all of them. In PHP attributes, the equivalent is to add a #[Route] attribute to the controller class. Read more about route groups and prefixes.

In addition to the options explained below, imports accept these route options: condition, controller, defaults, format, host, locale, methods, options, requirements, schemes, stateless and utf8. Their values are applied to every imported route and replace the values that those routes define for the same options (or for the same keys, in the case of defaults, options and requirements).

1
2
3
4
5
6
7
8
9
10
# config/routes.yaml
controllers:
    resource: ../src/Controller/
    type: attribute
    exclude: '../src/Controller/{Debug*Controller.php}'
    prefix: /blog
    trailing_slash_on_root: false
    name_prefix: blog_
    requirements:
        _locale: 'en|es|fr'

exclude

type: string | array default: null

One or more glob patterns of the files to ignore when importing the resource.

name_prefix

type: string default: null

A string added at the beginning of the name of all imported routes. It's called name-prefix in XML and it's set with the namePrefix() method in PHP.

prefix

type: string | array default: ''

A string added at the beginning of the path of all imported routes. Pass an array to define a different prefix per locale and create localized routes.

resource

type: string required

The resource to import: the path of a file or directory, a glob pattern or any other value supported by the route loaders.

trailing_slash_on_root

type: boolean default: true

When the path of an imported route is /, adding the prefix to it results in a trailing slash (e.g. /blog/). Set this option to false to remove that trailing slash (e.g. /blog). It's called trailing-slash-on-root in XML and it's set with the second argument of the prefix() method in PHP.

type

type: string default: null

The type of the imported resource. It's only needed when Symfony can't guess the loader from the resource (e.g. attribute to load the PHP attributes of controller classes).

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