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 Pass Extra Information from a Route to a Controller

How to Pass Extra Information from a Route to a Controller

Parameters inside the defaults collection don't necessarily have to match a placeholder in the route path. In fact, you can use the defaults array to specify extra parameters that will then be accessible as arguments to your controller, and as attributes of the Request object:

  • YAML
    1
    2
    3
    4
    5
    6
    7
    # config/routes.yaml
    blog:
        path:       /blog/{page}
        controller: App\Controller\BlogController::index
        defaults:
            page: 1
            title: "Hello world!"
    
  • 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="blog" path="/blog/{page}">
            <default key="_controller">App\Controller\BlogController::index</default>
            <default key="page">1</default>
            <default key="title">Hello world!</default>
        </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('blog', new Route('/blog/{page}', array(
        '_controller' => 'App\Controller\BlogController::index',
        'page'        => 1,
        'title'       => 'Hello world!',
    )));
    
    return $collection;
    

Now, you can access this extra parameter in your controller, as an argument to the controller method:

public function index($page, $title)
{
    // ...
}

Alternatively, the title could be accessed through the Request object:

1
2
3
4
5
6
7
8
use Symfony\Component\HttpFoundation\Request;

public function index(Request $request, $page)
{
    $title = $request->attributes->get('title');

    // ...
}

As you can see, the $title variable was never defined inside the route path, but you can still access its value from inside your controller, through the method's argument, or from the Request object's attributes bag.

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