How to Pass Extra Information from a Route to a Controller

Version: 2.5
Edit this page

Warning: You are browsing the documentation for Symfony 2.5, which is no longer maintained.

Read the updated version of this page for Symfony 6.0 (the current stable version).

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:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
# app/config/routing.yml
blog:
    path:      /blog/{page}
    defaults:
        _controller: AppBundle:Blog:index
        page:        1
        title:       "Hello world!"

Now, you can access this extra parameter in your controller:

1
2
3
4
public function indexAction($page, $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.

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