Routing

Routing in Action

A route is a map from a URL path to a controller. For example, suppose you want to match any URL like /blog/my-post or /blog/all-about-symfony and send it to a controller that can look up and render that blog entry. The route is simple:

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

The _controller parameter is a special key that tells Symfony which controller should be executed when a URL matches this route. The _controller string is called the logical name. It follows a pattern that points to a specific PHP class and method:

// src/Application/BlogBundle/Controller/BlogController.php
namespace Application\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class BlogController extends Controller
{
public function showAction($slug)
{
// use the $slug variable to query the database
$blog = ...;
return $this->render('BlogBundle:Blog:show.php.twig', array(
'blog' => $blog,
));
}
}