symfony Routing Default values for placeholders


Example

If you want to have a placeholder that may be omitted, you can give it a default value:

Using YAML:

# app/config/routing.yml
blog_list:
    path:      /blog/{page}
    defaults:  { _controller: AppBundle:Blog:list, page: 1 }
    requirements:
        page: '\d+'

Using Annotations:

// src/AppBundle/Controller/BlogController.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class BlogController extends Controller
{
    /**
     * @Route("/blog/{page}", name="blog_list", requirements={"page": "\d+"})
     */
    public function listAction($page = 1)
    {
        // ...
    }
}

In this example, both the /blog and /blog/1 URLs will match the blog_list route and will be handled by the listAction() method. In the case of /blog, listAction() will still receive the $page argument, with the default value 1.