Routing Wildcards

Routing is packed with cool little features, but the most common thing you'll see is the addition of wildcards. Add a /{count} to the end of the route's path:

7 lines modules/dino_roar/dino_roar.routing.yml
path: /the/dino/says/{count}
... lines 3 - 7

Because this is surrounded with curly-braces, the route will now match /the/dino/says/*anything*,

As soon as you have a routing wildcard called count, you can suddenly have a $count argument in your controller function:

15 lines modules/dino_roar/src/Controller/RoarController.php
... lines 1 - 6
class RoarController
public function roar($count)
... lines 11 - 12

The value for for the {count} part in the URL is passed to the $count argument. Both the wildcard and the argument must have the same name.

Use $count to change our scary greeting: $roar = 'R'.str_repeat('0', $count).'AR!'. Pass this to the Response:

15 lines modules/dino_roar/src/Controller/RoarController.php
... lines 1 - 8
public function roar($count)
$roar = 'R'.str_repeat('O', $count).'AR!';
return new Response($roar);
... lines 14 - 15

We just changed the route configuration, so we need to rebuild the routing cache:

drupal router:rebuild

Once I do, the /the/dino/says page returns a 404! Ah! As soon as you add /{count} to the route path, the route only matches when something is passed there. We need /the/dino/says/*something*, anything but blank. There are ways to make the wildcard optional - check out the Symfony routing docs.

Add 10 to the end of the URL:


Rooooooooooar! Make it 50 and the rooooooooooo....oooooooar gets longer.

Woah! You now know half of the new stuff in Drupal 8. It's this routing/controller layer. Make a route, then a controller and make that controller return a Response. Seriously, can we go on vacation now?

Leave a comment!

  • 2016-06-26 weaverryan

    Hey Enkhbilguun E.!

    Good question :). If you're using annotation routes (like in this course), give you argument a default value:

    public function showAction($genusName = 'foo')

    When you do that, when you go to http://domain/genus, it will *now* match your route and use this default.


  • 2016-06-26 Enkhbilguun E.

    How can we set a default value for the $genusName variable if http://domain/genus/{genusName} is empty?

  • 2016-05-18 weaverryan

    Hi Konrad!

    Apologies for my slow reply on your comments :). But now I've answered this question over on another comment:


  • 2016-05-14 Konrad ZajÄ…c

    Hi,about this use argument/
    I heve these two use statements:

    use Symfony\Component\BrowserKit\Response

    use Symfony\Component\HttpFoundation\Response;
    when I use symfonys autocompletion - I get the first one.
    But the second one is the one that works, could somone explain?
    sometimes it's the other way:
    use Symfony\Component\HttpKernel\Tests\Controller;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    symfony gave me the first, but the second one works