Archived video

This video is archived and won't be updated. We're keeping it online for now as a courtesy.

Alternate resources: 

Symfony Routing

Video loading...

  • 0:00
    Starting in Symfony2 Symfony Routing
  • 0:07
    with Leanna Pelham
  • 0:09
    Let's face it.
  • 0:11
    Every page needs a URL.
  • 0:13
    When you need a new page, we always start by creating a route, a chunk
  • 0:17
    of config that gives the page a URL.
  • 0:19
    In Symfony all routes are configured in just one file,
  • 0:22
  • 0:25
    Head back to your browser.
  • 0:26
    Input /hello/skywalker after app_dev.php.
  • 0:32
    The code behind this impressive page was
  • 0:34
    generated automatically in the new bundle.
  • 0:37
    You can change the last part of the URL to anything you want,
  • 0:40
    and it greets you politely.
  • 0:43
    The fact that this page works means that there's
  • 0:45
    a route somewhere that defines this URL pattern.
  • 0:48
    I already said that all routes live in routing.yml,
  • 0:51
    so it should be there.
  • 0:53
  • 0:54
    It's not here, but there is an event entry
  • 0:57
    that was added when we generated the bundle.
  • 1:00
    The resource key works like a PHP include.
  • 1:03
    Point at another routing file, and Symfony will put it in.
  • 1:06
    So even though Symfony only needs this one routing file,
  • 1:09
    we can pull in routes from anywhere.
  • 1:11
    So what's up with the @EventBundle magic?
  • 1:14
    The resource should just point to the path
  • 1:17
    of another file relative to this one.
  • 1:19
    But if the file lives in a bundle directory,
  • 1:21
    we can use the at (@) symbol and then the nickname we gave that bundle.
  • 1:26
    Since EventBundle lives at src/Yoda/EventBundle
  • 1:29
    that's where we'll find the imported file.
  • 1:33
    Ah-ha, we've found the missing route which
  • 1:36
    makes the /hello/skywalker page work.
  • 1:39
    The pattern is the URL, and the name of the pattern
  • 1:43
    acts like a wild card.
  • 1:44
    It means that any URL that looks like
  • 1:46
    /hello/start will match this route.
  • 1:50
    If we change "hello" to "there-is-another,"
  • 1:52
    the URL to the page changes.
  • 1:56
    Update the URL in your browser to see the moved page.
  • 2:04
    And then be cool and change the pattern back to /hello/name.
  • 2:14
  • 2:14
    So when you generate your bundle, your route
  • 2:17
    might have path instead of pattern-- scandal.
  • 2:20
    Here's the story.
  • 2:22
    Once upon a time, the Symfony elders renamed "pattern" to "path"
  • 2:26
    just because it's more semantically correct, and hey,
  • 2:30
    it's shorter anyways.
  • 2:31
    But "pattern" still works and will until Symfony 3.0.
  • 2:36
    Sorry, that's just about as scandalous
  • 2:37
    as things get around Symfony.
  • 2:40
    To be with the new, I'll change my routing to use "path."
  • 2:47
    The defaults _controller key is the second
  • 2:50
    critical piece of every route.
  • 2:52
    It tells Symfony which controller to execute when the route is matched.
  • 2:56
    But a controller is just a fancy word for a PHP function,
  • 3:00
    so you write this controller function
  • 3:02
    and Symfony executes it when the route is matched.
  • 3:05
    I know the EventBundle:Default:index controller doesn't look
  • 3:08
    like any function name you've ever met.
  • 3:11
    In reality, it's a top secret syntax with three different parts--
  • 3:15
    the bundle name, the controller class name, and the method name.
  • 3:19
    Symfony maps this to a controller class and method.
  • 3:23
    Oh, stop.
  • 3:24
    Let's stare at this for a few seconds
  • 3:26
    because we're going to see it a lot.
  • 3:29
    Notice that Symfony adds the word "Controller"
  • 3:31
    to the end of the class and "Action" to the end of the method name.
  • 3:35
    You'll probably hear the method name referred to as an action.
  • 3:39
    Open up the Controller class and find the indexAction method.
  • 3:43
    First check out the name variable that's passed as an argument
  • 3:46
    to the method.
  • 3:47
    This is sweet because the value of this argument
  • 3:50
    comes from the name wild card in our route.
  • 3:54
    So if I go to /hello/edgar, the name variable is "edgar."
  • 4:00
    When I go to /hello/skywalker, it's "skywalker."
  • 4:05
    And if we change "name" in the route to something else,
  • 4:07
    like "firstname," we'll see an error.
  • 4:13
  • 4:14
    So the name of the argument needs to match the name used in the route.
  • 4:22
    Now the route still has the same URL,
  • 4:24
    we're just giving the routing wild card a different name internally.
  • 4:33
    Let's get crazy by putting a second wild card in the route path.
  • 4:40
    When we refresh, we get a "No route found" error.
  • 4:43
    We need to put something for the count wild card,
  • 4:46
    otherwise it won't match our route.
  • 4:48
    Add /5 to the end to see the page.
  • 4:52
    Now that we have a count wild card in the route,
  • 4:54
    we can of course add a count argument to the action.
  • 4:58
    To prove everything's working, let's dump both arguments.
  • 5:07
    One neat thing is that the order of the arguments doesn't matter.
  • 5:11
    To prove it, swap the order of the arguments and refresh.
  • 5:15
    We've seen this twice now.
  • 5:17
    Symfony matches the routing wild cards to method arguments
  • 5:20
    by matching their names.
  • 5:22
    Remove the var_dump code so our page works again.
  • 5:26
    Routing is full of lots of cool tricks,
  • 5:28
    and we'll discover them along the way.
  • 5:31
    Are you wondering what other URLs your app might have?
  • 5:34
    Our friend Console can help you with that with the router debug command.
  • 5:38
    This shows a full list of every route in your app.
  • 5:41
    Right now, that means the ones we've been playing
  • 5:43
    with plus a few other internal Symfony debugging routes.
  • 5:47
    Be sure to remember this command.
  • 5:49
    It's your Swiss army knife for finding your way through a project.
Loading ...

Symfony Routing


When you need a new page, you always start by creating a route: a chunk of configuration that gives that page a URL. In Symfony, all routes are configured in just one file: app/config/routing.yml. Your route was generated automatically when you created the EventBundle. In this lesson we'll take a look at how this is working and explain basic routing concepts and route importing. We'll also dive into the _controller syntax, routing parameters, and controller arguments.

Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.