symfony3 Routing Routing using YAML


The routing configuration is included in your app/config/config.yml file, by default the app/config/routing.yml file.

From there you can link to your own routing configuration in a bundle

# app/config/routing.yml

    resource: "@AppBundle/Resources/config/routing.yml"

It might also contain several application global routes.

In your own bundle you can configure, routes which serve two purposes:

  • Matching against a request, such that the correct action is called for the request.
  • Generating an URL from the name and route parameters.

Below is an example YAML route configuration:

# src/AppBundle/Resources/config/routing.yml

    path: /application/content/page/{parameter}
        _controller: AppBundle:Default:myPage
        parameter: 42
        parameter: '\d+'
    methods: [ GET, PUT ]
    condition: "request.headers.get('User-Agent') matches '/firefox/i'"

The route is named my_page, and calls the myPageAction of the DefaultController in the AppBundle when requested. It has a parameter, named parameter with a default value. The value is only valid when it matches the regex \d+. For this route, only the HTTP methods GET and PUT are accepted. The condition is an expression in the example the route won't match unless the User-Agent header matches firefox. You can do any complex logic you need in the expression by leveraging two variables that are passed into the expression: context (RequestContext) and request (Symfony Request).

A generated route with the parameter value of 10 might look like /application/content/page/10.