Symfony Routing Basics: Difference between revisions
Jump to navigation
Jump to search
(Created page with "Category:Symfony Category:PHP Category:Web Development == Defining a route == Defining a route using annotations in a controller class: <syntaxhighlight lang="ph...") |
|||
| Line 50: | Line 50: | ||
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"`. | 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 | Note that placeholders are required. In the example above, the URL `/blog` would require its own separate route. | ||
Revision as of 21:10, 29 January 2015
Defining a route
Defining a route using annotations in a controller class:
// 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)
{
// ...
}
}
This basically creates a new page on the site.
Routes can alternatively be defined using Yaml, XML, or PHP, e.g.:
# app/config/config.yml
framework:
# ...
router: { resource: "%kernel.root_dir%/config/routing.yml" }
Wildcards in routes
Wildcards are defined with curly braces, e.g. {slug} below:
/**
* @Route("/blog/{slug}")
*/
public function showAction($slug)
{
// ...
}
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.