Archived video

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

Alternate resources: 

Using a Service in Symfony 3

Video loading...

  • 0:04
    Using a Service in Symfony 3 Ryan Weaver
  • 0:08
    OK.
  • 0:09
    First, a little setup.
  • 0:11
    In the template, remove the Fun Fact text
  • 0:13
    and move it into GenusController
  • 0:16
    by creating a new $funFact variable.
  • 0:19
    Let's make things interesting
  • 0:21
    by adding some asterisks around three-tenths.
  • 0:26
    Markdown should eventually turn that into italics.
  • 0:32
    Pass $funFact into the template,
  • 0:34
    and render it with the normal
  • 0:35
    {{ funFact }}.
  • 0:38
    When we refresh the browser,
  • 0:40
    we have the exact same text,
  • 0:42
    but with the unparsed asterisk.
  • 0:46
    Now, how the heck can we use
  • 0:48
    the new markdown.parser service
  • 0:50
    to turn those asterisks into italics?
  • 0:53
    Remember, we have access to the container
  • 0:56
    from inside a controller.
  • 0:58
    Start with $funFact = $this->container->get('markdown,.parser')
  • 1:06
    Now, we have that parser object,
  • 1:09
    so we can call a method on it.
  • 1:11
    The one we want is transform.
  • 1:14
    Pass that, the string to parse.
  • 1:16
    Wait,
  • 1:17
    so,
  • 1:18
    how did I know this object has a transform method?
  • 1:22
    Well, a few ways.
  • 1:24
    First, PhpStorm knows what the markdown.parser object is,
  • 1:29
    so it gives me autocompletion.
  • 1:31
    But you can always read the documentation of the bundle.
  • 1:35
    It will tell you how to use any services it gives you.
  • 1:39
    OK, team,
  • 1:40
    time to try this out.
  • 1:42
    Refresh.
  • 1:43
    And hey, it's working.
  • 1:45
    OK, it's not exactly working.
  • 1:49
    Open the page source.
  • 1:52
    It looks like the parser is working its magic,
  • 1:55
    but the HTML tags are being escaped
  • 1:58
    into HTML entities.
  • 2:01
    This is Twig at work.
  • 2:03
    One of the best features of Twig
  • 2:05
    is that it automatically escapes
  • 2:07
    any HTML that you render.
  • 2:09
    That gives you free security from XSS attacks,
  • 2:13
    and for those few times when you do want to print HTML,
  • 2:17
    just add the | raw filter.
  • 2:20
    Refresh again.
  • 2:23
    Ahh.
  • 2:23
    It's rendering in some lovely italics.
  • 2:26
    Oh, one more thing.
  • 2:28
    We can actually do all of this with less code.
  • 2:31
    In the controller, replace
  • 2:33
    this->container->get
  • 2:36
    with just this->get.
  • 2:39
    This does the same thing as before.
  • 2:42
    OK, here's the big, giant, important takeaway so far,
  • 2:47
    why do we add bundles to our app?
  • 2:50
    Because bundles put more services in our container,
  • 2:54
    and services are tools.
  • 2:58
    So my question now is,
  • 3:00
    how can we configure these services,
  • 3:03
    so that they do exactly what we need them to?

Using a Service in Symfony 3

Loading...

In this lesson, you'll learn how to use the new service that you installed through a bundle in the previous lesson.

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.