Editing
Symfony Routing Basics
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Defining a route == Defining a route using annotations in a controller class:<ref>[http://symfony.com/doc/current/book/routing.html#basic-route-configuration Basic Route Configuration], Symfony routing documentation</ref> <syntaxhighlight lang="php" highlight="5,10"> // 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}") */ public function showAction($slug) { // ... } } </syntaxhighlight> This basically creates a new page on the site. Routes can alternatively be defined using Yaml, XML, or PHP, e.g.: <syntaxhighlight lang="yaml"> # app/config/config.yml framework: # ... router: { resource: "%kernel.root_dir%/config/routing.yml" } </syntaxhighlight> === Wildcards in routes === Wildcards are defined with curly braces, e.g. `{slug}` below:<ref>[http://symfony.com/doc/current/book/routing.html#routing-with-placeholders Routing with Placeholders], Symfony routing documentation</ref> <syntaxhighlight lang="php"> /** * @Route("/blog/{slug}") */ public function showAction($slug) { // ... } </syntaxhighlight> This route will match `/blog/*`. Any value placed after `/blog/` will be passed to the method as the value of the `$slug` variable. E.g. a request for `/blog/hello-world` will cause value of `$slug` to be `"hello-world"`. Note that placeholders are required. In the example above, the URL `/blog` would require its own separate route. === Default wildcard values === In effect, the `defaults` attribute of `@Route` makes wildcard values optional in URLs:<ref>[http://symfony.com/doc/current/book/routing.html#required-and-optional-placeholders Required and Optional Placeholders], Symfony routing documenation</ref> <syntaxhighlight lang="php"> /** * @Route("/blog/{page}", defaults={"page"=1}) */ public function showAction($page) { // ... } </syntaxhighlight> If a page value is not supplied in the URL, `$page` will have a default value of `1`. === Restricting wildcard values === The `requirements` attribute of the `@Route` annotation enforces formatting of wildcard values with regular expressions.<ref>[http://symfony.com/doc/current/book/routing.html#adding-requirements Adding Requirements], Symfony routing documentation]</ref> <syntaxhighlight lang="php"> /** * @Route("/blog/{page}", defaults={"page"=1}, requirements={"page"="\d+"}) */ public function showAction($page) { // ... } </syntaxhighlight> Now only integer values will match the route. A separate route could be created for string values. === HTTP method requirements === Method requirements are set with the `@Method` annotation: `@Method("GET")`, `@Method("POST")`, etc.<ref>[http://symfony.com/doc/current/book/routing.html#adding-http-method-requirements Adding HTTP Method Requirements], Symfony routing documentation</ref>
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information