Symfony Routing Basics: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
No edit summary
Line 2: Line 2:
== Defining a route ==
== Defining a route ==


Defining a route using annotations in a controller class:
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">
<syntaxhighlight lang="php" highlight="5,10">
Line 36: Line 36:
=== Wildcards in routes ===
=== Wildcards in routes ===


Wildcards are defined with curly braces, e.g. `{slug}` below:
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">
<syntaxhighlight lang="php">
Line 54: Line 54:
=== Default wildcard values ===
=== Default wildcard values ===


In effect, the `defaults` attribute of `@Route` makes wildcard values optional in URLs:
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">
<syntaxhighlight lang="php">
Line 70: Line 70:
=== Restricting wildcard values ===
=== Restricting wildcard values ===


The `requirements` attribute of the `@Route` annotation enforces formatting of wildcard values with regular expressions.  
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">
<syntaxhighlight lang="php">
Line 83: Line 83:


Now only integer values will match the route. A separate route could be created for string values.
Now only integer values will match the route. A separate route could be created for string values.
== Notes ==
<references />

Revision as of 21:26, 29 January 2015

Defining a route

Defining a route using annotations in a controller class:[1]

// 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:[2]

/**
     * @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.

Default wildcard values

In effect, the defaults attribute of @Route makes wildcard values optional in URLs:[3]

/**
     * @Route("/blog/{page}", defaults={"page"=1})
     */
    public function showAction($page)
    {
        // ...
    }

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.[4]

/**
     * @Route("/blog/{page}", defaults={"page"=1}, requirements={"page"="\d+"})
     */
    public function showAction($page)
    {
        // ...
    }

Now only integer values will match the route. A separate route could be created for string values.

Notes

  1. Basic Route Configuration, Symfony routing documentation
  2. Routing with Placeholders, Symfony routing documentation
  3. Required and Optional Placeholders, Symfony routing documenation
  4. Adding Requirements, Symfony routing documentation]