symfony Routing Routes with placeholders


Example

Using YAML:

# app/config/routing.yml
blog_show:
    path:     /blog/{slug}
    defaults: { _controller: AppBundle:Blog:show }

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/{slug}", name="blog_show")
     */
    public function showAction($slug)
    {
        // ...
    }
}

Any request with a URL matching /blog/* will be handled by the showAction() method of the BlogController within AppBundle. The controller action will receive the value of the placeholder as a method argument.

For example, a request for /blog/my-post will trigger a call to showAction() with an argument $slug containing the value my-post. Using that argument, the controller action can change the response depending on the value of the placeholder, for instance by retrieving the blog post with the slug my-post from the database.