Buy

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
dino_says:
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:

http://localhost:8000/the/dino/says/10

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-12-04 Brian Baquiran

    Haha, I guess that's my initial contribution to the platform :D

    Thanks weaverryan!

  • 2016-12-03 weaverryan

    Hey Brian!

    Wow, so this blew my mind a little bit :). And after some digging, you seem to have uncovered a bug recently introduced into Symfony. I've created an issue about it here - https://github.com/symfony/sym... - we'll find out if this is an intended change or not. But, the most important thing is that you are NOT crazy, in fact, you did a great job of following through on this very weird behavior.

    So, keep going on the tutorial, and don't let this nag you anymore. When I first tried your code, I thought I was losing my mind!

    Cheers!

  • 2016-12-02 Brian Baquiran

    Hey weaverryan,
    You can check it here: https://github.com/brianbaquir...

    I did a dump($genusName) and Symfony displays it as null.

    I am using bin/console server:run.

    In the Symfony Profiler's Request/Response screen, _route_params shows "genusName2" => "octopus"

    It's not stopped me from working through the rest of the course, but it bothers me that it's not producing an error when it should.

  • 2016-12-02 weaverryan

    Hey Brian!

    Yea, good find on changing the annotation use statement for Route. It's one of those situation where there are multiple "Route" classes, so PhpStorm just suggests them all :).

    So, 2 things:

    A) Can you post your full controller code here?
    B) Are you using the built-in PHP web server (e.g. bin/console server:run)? If not, and you're using your own web-server, make sure to prefix the URL with app_dev.php. So, instead of going to http://mysite.local/genus/octopus, go to http://mysite.local/app_dev.php/genus/octopus. This activates the "dev" environment (which is activated automatically if you use the built-in web server). If that's the problem - we have more on that in the next screencast :) http://knpuniversity.com/scree...

    Cheers!

  • 2016-12-02 Brian Baquiran

    Hi! Yes, the code is exactly what's in the video and script. I'm using annotations, as directed in the tutorial. I haven't set a default value.

    I did notice that the first time I tried it, the @Route annotation was autocompleted as Symfony\Component\Routing\Annotation\Route and not the Sensio\Bundle\FrameworkExtraBundle\Configuration\Route in the tutorial. It also worked, and I didn't notice the difference until trying to figure out why it didn't produce the error. I've set it to use the Sensio one, but still no joy.

    Anything else I can check?

  • 2016-12-02 weaverryan

    Hey Brian Baquiran !

    Hmm... Does your code look exactly like mine, or do you have some differences? For example, if you have {genusName2} in the route, but $genusName as your argument, did you possibly give it a default value (e.g. $genusName = null)? Are you also using annotations routes (or are you possibly using YAML)?

    Lemme know - there's gotta be some tiny detail that's mucking things up :).

    Cheers!

  • 2016-12-02 Brian Baquiran

    I'm not getting the error message when the wildcard and variable name don't match, just "The genus: "

  • 2016-11-12 weaverryan

    Hey @Jaroslav!

    Thanks! :). You didn't miss the assets:install - we never ran it - but I believe that the installer handles this for you (it's also run automatically when you run "composer install"). But, if something went wrong, it's possible you need to run this.

    But, if you're getting a white screen (white screen of death! I call it) - then that's a different problem. The most likely cause is that you're not in the *dev* environment in Symfony. If you're using the built-in PHP web server that we use (i.e. bin/console server:run) then you will get this automatically. But if you're using your own web server, like Apache or Nginx, then you'll need to modify your URL, e.g. if you setup a host name called mydomain.local, then you would go to http://mydomain.local/app_dev.php for the homepage, or http://mydomain.local/app_dev.php/genus for the /genus page. The extra app_dev.php activates the "dev" environment. We talk about this is the second course of the Symfony series :) http://knpuniversity.com/scree...

    Let me know if that's the problem, or if it's something else!

    Cheers!

  • 2016-11-12 Jaroslav

    Hi! Very nice tutorial, just missed the information that I should probably run assets:install in some of previous steps (to get as nice debug page as you get) and now I am in trouble with renaming genusName to genusName2. I dont get any error, just white page with "Genus name: " string. Why I do not get an error page, plase?

  • 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.

    Cheers!

  • 2016-06-26 Enkhbilguun E.

    Hi,
    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: https://knpuniversity.com/scre...

    Cheers!

  • 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?
    -----edit-----
    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