How to Define Route Requirements
Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.2 (the current stable version).
Route requirements can be used to make a specific route
only match under specific conditions. The simplest example involves restricting
a routing {wildcard}
to only match some regular expression:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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)
{
// ...
}
}
Thanks to the \d+
requirement (i.e. a "digit" of any length), /blog/2
will
match this route but /blog/some-string
will not match.
Since the parameter requirements are regular expressions, the complexity and flexibility of each requirement is entirely up to you. Suppose the homepage of your application is available in two different languages, based on the URL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// src/AppBundle/Controller/MainController.php
// ...
class MainController extends Controller
{
/**
* @Route("/{_locale}", defaults={"_locale"="en"}, requirements={
* "_locale"="en|fr"
* })
*/
public function homepageAction($_locale)
{
}
}
For incoming requests, the {_locale}
portion of the URL is matched against
the regular expression (en|fr)
.
Path | Parameters |
---|---|
/ |
{_locale} = "en" |
/en |
{_locale} = "en" |
/fr |
{_locale} = "fr" |
/es |
won't match this route |
Tip
The route requirements can also include container parameters, as explained in this article. This comes in handy when the regular expression is very complex and used repeatedly in your application.
Adding HTTP Method Requirements
In addition to the URL, you can also match on the method of the incoming request (i.e. GET, HEAD, POST, PUT, DELETE). Suppose you create an API for your blog and you have 2 routes: One for displaying a post (on a GET or HEAD request) and one for updating a post (on a PUT request). This can be accomplished with the following route configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// src/AppBundle/Controller/BlogApiController.php
namespace AppBundle\Controller;
// ...
class BlogApiController extends Controller
{
/**
* @Route("/api/posts/{id}", methods={"GET","HEAD"})
*/
public function showAction($id)
{
// ... return a JSON response with the post
}
/**
* @Route("/api/posts/{id}", methods={"PUT"})
*/
public function editAction($id)
{
// ... edit a post
}
}
Despite the fact that these two routes have identical paths
(/api/posts/{id}
), the first route will match only GET or HEAD requests and
the second route will match only PUT requests. This means that you can display
and edit the post with the same URL, while using distinct controllers for the
two actions.
Note
If no methods
are specified, the route will match on all methods.
Tip
If you're using HTML forms and HTTP methods other than GET
and POST
,
you'll need to include a _method
parameter to fake the HTTP method. See
How to Change the Action and Method of a Form for more information.
Adding a Host Requirement
You can also match on the HTTP host of the incoming request. For more information, see How to Match a Route Based on the Host in the Routing component documentation.
Adding Dynamic Requirements with Expressions
For really complex requirements, you can use dynamic expressions to match any information on the request. See How to Restrict Route Matching through Conditions.