Symfony 4: Routes, Controllers, Pages, oh my!

Video loading...

  • 0:03
    Symfony 4: Routes, Controllers, Pages, oh my! with Ryan Weaver
  • 0:08
    Let’s create our first page.
  • 0:10
    Actually, this is the main job of a framework,
  • 0:14
    to give you a route and controller system.
  • 0:18
    A route is configuration that defines the URL for a page,
  • 0:22
    and a controller is a function that we write
  • 0:25
    that actually builds the content for that page.
  • 0:29
    And right now, our app is really small,
  • 0:34
    instead of weighing down your project with every possible feature
  • 0:38
    you could ever need.
  • 0:40
    We’re not in zero gravity yet.
  • 0:42
    A Symfony app is basically just a small route controller system.
  • 0:48
    Later, we’ll install more features when we need them,
  • 0:51
    like a work drive – those always come in handy.
  • 0:55
    Adding more features is actually going to be pretty awesome.
  • 0:59
    More on that later.
  • 1:01
    Open your app’s main routing file,
  • 1:04
    config/routes.yaml.
  • 1:07
    Hey! We already have an example.
  • 1:10
    Uncomment that.
  • 1:12
    Ignore the index key for now. That’s the internal name of the route,
  • 1:17
    but it’s not important yet.
  • 1:19
    This says that when someone goes to the homepage/, Symfony
  • 1:23
    should execute in index method in a DefaultController class.
  • 1:29
    Change this to ArticleController, and the method to homepage.
  • 1:33
    And, yeah, that’s a route!
  • 1:37
    Hi, route!
  • 1:38
    It defines the URL and tells Symfony what controller function to execute.
  • 1:44
    The Controller class doesn’t exist yet, so let’s create it.
  • 1:48
    Right click on the Controller directory and go to New,
  • 1:51
    or press Command+N on a Mac. Choose PHP Class.
  • 1:56
    And yes!
  • 1:58
    Remember that Composer setup we did in preferences?
  • 2:01
    Thanks to that,
  • 2:02
    PHP Storm correctly guesses the namespace.
  • 2:06
    The force is strong with this one.
  • 2:09
    The namespace for every class
  • 2:11
    and source should be App + whatever subdirectory it’s in.
  • 2:16
    Name this ArticleController,
  • 2:21
    and inside, add public function_homepage().
  • 2:25
    This function is the controller, and it’s our place to build the page.
  • 2:31
    To be more confusing,
  • 2:33
    it’s also called an action or "ghob!" to its Klingon friends.
  • 2:39
    Anyways, we can do whatever we want here –
  • 2:42
    make database queries, API calls,
  • 2:45
    take soil samples looking for organic materials,
  • 2:48
    or render a template.
  • 2:50
    There's just 1 rule:
  • 2:52
    a controller must return a Symfony response object.
  • 2:58
    So, let’s say, return_new Response.
  • 3:02
    We want the one from HttpFoundation.
  • 3:05
    Give it a calm message –
  • 3:08
    OMG! My first page already! Wooo!
  • 3:13
    Oh, and check this out.
  • 3:14
    When I let PHPStorm autocomplete the response class,
  • 3:18
    it added this use statement to the top of the file automatically.
  • 3:22
    You'll see me do that a lot.
  • 3:24
    Good job, Storm!
  • 3:26
    Let’s try the page.
  • 3:27
    Find your browser.
  • 3:29
    Oh, this welcome page only shows if you don’t have
  • 3:32
    any routes configured.
  • 3:34
    Refresh.
  • 3:35
    Yes! This is our page; our first of many.
  • 3:41
    That was pretty easy, but it can be easier.
  • 3:45
    Instead of creating our routes in YAML,
  • 3:48
    let’s use a cool feature called annotations.
  • 3:51
    This is an extra feature, so we need to install it.
  • 3:55
    Find your open terminal and run, composer require annotations.
  • 4:01
    Interesting!
  • 4:02
    This annotations package
  • 4:05
    actually installed sensio/framework-extra-bundle.
  • 4:09
    We’re going to talk about how that works very soon.
  • 4:13
    Now, about these annotation routes.
  • 4:16
    Comment out the yaml route.
  • 4:19
    Then, in ArticleController, above the controller method, add /**,
  • 4:25
    hit Enter, clear this out,
  • 4:27
    and say, @Route.
  • 4:30
    You can use either class,
  • 4:31
    but make sure PHPStorm adds the use statement on top -
  • 4:35
    then add (“/”).
  • 4:39
    That’s it!
  • 4:41
    The route is defined right above the controller,
  • 4:45
    which is why I love annotation routes.
  • 4:48
    Everything is in 1 place.
  • 4:50
    But don’t trust me, find your browser and refresh.
  • 4:54
    It’s a trap! I mean, it works!
  • 4:57
    So, what else can we do with routes?
  • 5:00
    Create another public function called show().
  • 5:03
    I want this page to eventually display a full article.
  • 5:09
    Give it a route –
  • 5:10
    @Route(“/news/why-asteroids- taste-like-bacon”).
  • 5:16
    Eventually, this is how we want our URLs to look.
  • 5:20
    This is called a slug.
  • 5:22
    It’s a URL version of the title.
  • 5:25
    As usual, return a new Response
  • 5:28
    (‘Future page to show one space article!’);
  • 5:33
    Perfect!
  • 5:34
    Copy that URL and try it in your browser.
  • 5:39
    It works, but this sucks.
  • 5:43
    I don’t want to build a route and controller for every single article
  • 5:47
    that lives in the database.
  • 5:49
    Nope, we need a route that can match
  • 5:51
    /news/anything. How? Use {slug}.
  • 5:59
    This route now matches /news/anything.
  • 6:03
    That {slug} is a wildcard.
  • 6:08
    Oh, and the name slug could be anything,
  • 6:11
    but whatever you choose now becomes available as an argument
  • 6:15
    to your "ghob!".
  • 6:17
    I mean, your action.
  • 6:19
    So, let’s refactor our success message to say
  • 6:22
    ‘Future page to show the article:’
  • 6:24
    and then that slug.
  • 6:29
    Try it!
  • 6:31
    Refresh the same URL.
  • 6:33
    Yes!
  • 6:34
    It matches the route and the slug prints.
  • 6:38
    Change it to something else –
  • 6:39
    why-asteroids-taste-like-tacos.
  • 6:43
    So delicious!
  • 6:45
    Go back to bacon because, you know,
  • 6:48
    everybody knows that’s what asteroids really taste like.
  • 6:52
    And yes, we’re 3 chapters in,
  • 6:55
    and you now know the first half of Symfony –
  • 6:58
    the route and controller system.
  • 7:01
    Sure, you can do fancier things with routes,
  • 7:04
    like match regular expressions,
  • 7:06
    http methods, or host names,
  • 7:09
    but that will all be pretty easy for you now.
  • 7:12
    It’s time to move on to something really important.
  • 7:16
    It’s time to learn about Symfony Flex and the Recipe system.
  • 7:21
    Yum!

Symfony 4: Routes, Controllers, Pages, oh my!

Loading...

Let's create our first page in our Symfony 4 app and learn about routes and controllers in the process.

Downloads: 
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.