Symfony 4: Services

Video loading...

  • 0:03
    Symfony 4: Services with Ryan Weaver
  • 0:07
    It’s time to talk about the most fundamental part
  • 0:10
    of Symfony: Services.
  • 0:13
    Honestly, Symfony is nothing more than a bunch
  • 0:16
    of useful objects that work together.
  • 0:19
    For example, there is a router object that matches routes
  • 0:23
    and generates URLs.
  • 0:24
    There's a Twig object that renders templates
  • 0:27
    and there's a logger object that Symfony is already using internally
  • 0:32
    to store things in a var/log/dev.log file.
  • 0:37
    Actually, everything in Symfony,
  • 0:40
    I mean everything, is done by one of these useful objects.
  • 0:46
    And these useful objects have a special name, Services.
  • 0:51
    But don’t get too excited about that word, Service.
  • 0:55
    It’s a special word for a really simple idea.
  • 0:59
    A service is any object that does work, like generating URLs,
  • 1:05
    sending e-mails, or saving things to a database.
  • 1:10
    Symfony comes with a huge number of services,
  • 1:13
    and I want you to think of services as your tools.
  • 1:18
    Like, if I gave you the Logger service, or object,
  • 1:22
    then you could use it to log messages.
  • 1:25
    If I gave you a Mailer service, you could send some e-mails.
  • 1:30
    Tools – the entire second half of Symfony is all about learning
  • 1:35
    where to find these services and how to use them.
  • 1:39
    Every time you learn about a new service,
  • 1:41
    you get a new tool,
  • 1:43
    and become just a little bit more dangerous.
  • 1:47
    Let’s check out the logging system.
  • 1:50
    Find your terminal, and run, tail –f var/log/dev.log.
  • 1:57
    I’ll clear the screen.
  • 2:00
    Now, refresh the page and move back.
  • 2:04
    Awesome!
  • 2:06
    This proves that Symfony has some sort of logging system,
  • 2:11
    and since everything is done by a service,
  • 2:14
    there must be a logger object.
  • 2:18
    So, here’s the question,
  • 2:19
    how can we get the logger service so that we can log our own messages?
  • 2:27
    Here’s the answer.
  • 2:28
    Inside the controller, on the method, add an additional argument.
  • 2:33
    Give it a logger interface type hint, hit tab to autocomplete that,
  • 2:39
    and call it whatever you want.
  • 2:41
    How about logger?
  • 2:44
    Remember, when you autocomplete,
  • 2:46
    PhpStorm adds the use statement to the top for you.
  • 2:50
    Now, we can use one of its methods:
  • 2:53
    logger->info (‘Article is being hearted’):
  • 2:59
    Before we talk about this, let’s try it.
  • 3:02
    Find your browser and click the heart.
  • 3:05
    That hit the Ajax endpoint.
  • 3:08
    Go back to the terminal.
  • 3:09
    Yes!
  • 3:10
    There it is at the bottom.
  • 3:12
    Hit CTRL+C to exit tail.
  • 3:15
    OK cool!
  • 3:16
    But how the heck did that work?
  • 3:20
    Here’s the deal.
  • 3:22
    Before Symfony executes our controller, it looks at each argument.
  • 3:28
    For simple arguments like $slug,
  • 3:30
    it passes us the wildcard value from the router.
  • 3:34
    But for logger, it looks at the type hint and realizes
  • 3:39
    that we want Symfony to pass us the logger object.
  • 3:44
    Oh, and the order of the arguments does not matter.
  • 3:49
    This is a very powerful idea called Autowiring.
  • 3:54
    If you need a service object,
  • 3:56
    you just need to know the correct type hint to use.
  • 4:00
    So, how the heck did I know to use logger interface?
  • 4:04
    Well, of course,
  • 4:06
    if you look at the official Symfony docs about the logger,
  • 4:09
    it'll tell you, but there is a cooler way.
  • 4:13
    Go to your terminal and run, ./bin/console debug:autowiring.
  • 4:19
    Boom!
  • 4:20
    This is a full list of all of the type hints
  • 4:24
    that you can use to get a service.
  • 4:27
    Notice that most of them say that they are an alias to something.
  • 4:31
    Don’t worry about that too much.
  • 4:34
    Like routes, each service
  • 4:36
    has an internal name you can use to reference it.
  • 4:39
    We’ll learn more about that later.
  • 4:42
    oh, and whenever you install a new package,
  • 4:45
    you’ll get more and more services in this list – more tools.
  • 4:50
    And check this out,
  • 4:51
    if you want to get the Twig service,
  • 4:54
    you can use either of these 2 type hints.
  • 4:57
    And remember how I said that everything in Symfony
  • 5:00
    is done by a service?
  • 5:01
    Well, when we call $this ->render in a controller,
  • 5:06
    that’s just a short cut
  • 5:08
    to fetch the Twig service and call a method on it.
  • 5:12
    In fact, let’s pretend
  • 5:14
    that the $this ->render shortcut does not exist,
  • 5:18
    how could we render a template?
  • 5:21
    No problem!
  • 5:22
    We just need the Twig service.
  • 5:25
    Add a second argument with an Environment type hint
  • 5:29
    because that’s the class name we saw in debug:autowiring.
  • 5:33
    Call the arg $twigEnvironment.
  • 5:36
    Next, change the return statement to be $html = twigEnvironment->render.
  • 5:44
    The method we want to call on the Twig object is coincidentally
  • 5:48
    the same as the controller shortcut.
  • 5:51
    Then at the bottom, return_new Response and pass $html
  • 5:57
    OK, this is way more work than before,
  • 6:00
    and I would not do this in a real project,
  • 6:04
    but I wanted to prove a point.
  • 6:06
    When you use the $this->render shortcut method on the controller,
  • 6:11
    all it really does is call render on the Twig service
  • 6:16
    and then wrap it inside a response object for you.
  • 6:20
    Try it.
  • 6:21
    Go back and refresh the page.
  • 6:25
    It works exactly like before.
  • 6:29
    Of course, we will use shortcut methods
  • 6:32
    because they make our life way more awesome.
  • 6:36
    I’ll change my code back to look like it did before.
  • 6:41
    But the point is this: everything is done by a service.
  • 6:45
    If you learn to master services,
  • 6:47
    you can do anything from anywhere in Symfony.
  • 6:53
    There's a lot more to say about the topic of services
  • 6:55
    and so many other parts of Symfony:
  • 6:58
    Configuration, Doctrine and the Database, Forms, Security,
  • 7:03
    and APIs, just to name a few.
  • 7:06
    The Space Bar is far from being the galactic information source
  • 7:10
    that we know it will be.
  • 7:12
    But congrats,
  • 7:14
    you just spent an hour getting an awesome foundation in Symfony.
  • 7:18
    You will not regret your hard work.
  • 7:21
    You're on your way to building great things,
  • 7:23
    and as always, becoming a better and better developer.

Symfony 4: Services

Loading...

In this tutorial, we'll learn about useful objects in Symfony known as services.

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.