Archived video

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

Alternate resources: 

Create a Service in Symfony 3

Video loading...

  • 0:04
    Create a Service in Symfony 3 Ryan Weaver
  • 0:08
    Hey, friends, back for more?
  • 0:10
    You should be feeling pretty good about yourself already,
  • 0:13
    but you're about to feel like a kid in a very nerdy,
  • 0:17
    borderline embarrassing, candy store.
  • 0:20
    In this course, we fully uncover Services,
  • 0:24
    those little useful objects that literally do everything in Symfony.
  • 0:30
    Now as always, you should code along with me,
  • 0:33
    otherwise we'll find out and it's kind of company policy
  • 0:37
    to mail you a big package full of glitter.
  • 0:40
    But if you do code along, and we meet in person,
  • 0:43
    well, there's a cookie with your name on it.
  • 0:47
    So find the Download button on any of the tutorial pages,
  • 0:50
    download the code, unzip it, and move into the start directory.
  • 0:55
    As always, I already have the start directory code here,
  • 0:58
    so I'll skip to the last step. Open up a new terminal
  • 1:02
    and launch the built-in PHP web server with:
  • 1:04
    php bin/console server:run
  • 1:08
    In your case, open up the README file in the start directory;
  • 1:12
    it has a few extra instructions.
  • 1:15
    In the last courses,
  • 1:17
    if I repeated anything too many times, it was this, "Refresh!"
  • 1:22
    But second would be that Symfony is basically just a big container
  • 1:27
    of useful objects called services
  • 1:30
    and everything that happens is actually done by one of these.
  • 1:34
    For example, the render function,
  • 1:36
    it's the key to rendering templates, right?
  • 1:40
    No, it's a fraud!
  • 1:42
    Open up Symfony's baseController
  • 1:45
    it doesn't do any work,
  • 1:47
    it just finds the Templating service
  • 1:49
    and tells it to do all the work.
  • 1:52
    Ah, management.
  • 1:54
    This means that Symfony doesn't render templates,
  • 1:57
    the Templating service renders templates.
  • 2:00
    To get a big list of birthday wishes, I mean services,
  • 2:04
    run ./bin/console debug:container
  • 2:09
    That's a lot of firepower at our fingertips.
  • 2:14
    We also found out that we can control these services
  • 2:17
    in app/config/config.yml.
  • 2:23
    But now we have a new, daring goal,
  • 2:26
    adding our own services to the container.
  • 2:30
    It turns out, learning to do this will unlock almost
  • 2:33
    everything else in Symfony.
  • 2:36
    Open up GenusController and look at showAction.
  • 2:40
    We used to have about 15 lines of code that parsed the $funFact
  • 2:43
    through Markdown and then cached it.
  • 2:45
    I want that back.
  • 2:48
    But, I don't want to have these
  • 2:50
    15 lines of code live here in my controller.
  • 2:53
    Why not?
  • 2:54
    3 reasons.
  • 2:56
    First, I can't re-use this.
  • 2:59
    If I need to do parse some markdown somewhere else,
  • 3:02
    well, I could copy and paste,
  • 3:04
    but then, how would I sleep at night?
  • 3:07
    Second, it's not instantly clear what these 15 lines do.
  • 3:11
    I have to actually take time and read them to find out.
  • 3:16
    If you have a lot of chunks like this,
  • 3:18
    suddenly nobody knows what's going on in your app.
  • 3:22
    You know what I'm talking about?
  • 3:24
    And third, if you want to unit test this code, well, you can't.
  • 3:29
    To unit test something, it needs to live in its own,
  • 3:33
    isolated, focused class.
  • 3:36
    Well, hey, that's a great idea.
  • 3:39
    Let's move this outside of our controller
  • 3:41
    and solve all 3 problems at once,
  • 3:45
    plus, impress our developer friends with our sweet code organizational skills.

Create a Service in Symfony 3

Loading...

In this lesson, we'll explore what problems a services architecture can solve. You'll learn how services can unlock a bunch of powerful features for you in Symfony 3.

New to Symfony 3 or need a brush up on Symfony concepts? Check out these series first and then head back here to dive into Services and the Container.

Note: To set up the project and code along, see the instructions in start/README.md.

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.