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 Class in Symfony 3

Video loading...

  • 0:04
    Create a Service Class in Symfony 3 Ryan Weaver
  • 0:08
    Ready to move a chunk of code out of the controller?
  • 0:11
    Well, good for you.
  • 0:12
    Step 1, create a new PHP class.
  • 0:17
    In AppBundle, I'll create a new directory called Service,
  • 0:21
    but that could be called anything.
  • 0:24
    Inside, add a new PHP class called MarkdownTransformer.
  • 0:28
    If you're keeping score at home,
  • 0:30
    that could also be called anything.
  • 0:34
    Start with 1 public function, parse, with an $str argument.
  • 0:39
    Eventually, this will do all the dirty work of markdown parsing and caching.
  • 0:44
    But for now, keep it simple and return strtoupper($str).
  • 0:50
    But use your imagination,
  • 0:53
    pretend like it totally is awesome and it is parsing our markdown.
  • 0:59
    In fact, it's so awesome that we want to use it in our controller.
  • 1:03
  • 1:05
    First, create a new object with
  • 1:07
    $transformer = new MarkdownTransformer.
  • 1:12
    Nothing special here.
  • 1:15
    The new method is purposefully not static,
  • 1:19
    and this means we need to instantiate the object first.
  • 1:23
    Next, add $funFact = $transformer->parse
  • 1:28
    and pass ($genus->getFunFact())
  • 1:31
    And that's it.
  • 1:32
    If you're feeling massively underwhelmed,
  • 1:35
    you're right where I want you.
  • 1:37
    I want this to be boring and easy.
  • 1:39
    There are fireworks and candy and ice cream
  • 1:43
    and other exciting stuff later, I promise.
  • 1:46
    Finish this by passing $funFact into the template
  • 1:49
    so we can render the parsed version.
  • 1:52
    Then, open the template and replace genus.funFact with just funFact.
  • 1:59
    Try it out. Open up localhost:8000/genus
  • 2:05
    Then click one of the genuses.
  • 2:07
  • 2:08
    The fun fact is screaming at us in upper case.
  • 2:13
    So believe it or not, you just saw one of the most important
  • 2:18
    and commonly confusing object-oriented strategies
  • 2:21
    that exist anywhere, in any language.
  • 2:24
    And it's this, you should take chunks of code that do things
  • 2:29
    and move them into an outside function in an outside class.
  • 2:34
    That's it.
  • 2:36
    Oh, and guess what?
  • 2:37
    MarkdownTransformer is a service.
  • 2:41
    Because remember, a service is just a class that does work for us.
  • 2:46
    And when you isolate a lot of your code into these service classes,
  • 2:50
    you start to build what's called a service-oriented architecture.
  • 2:58
    That basically means that instead of having all of your code in big controllers,
  • 3:03
    you organize them into nice little service classes that each do 1 job.
  • 3:09
    Of course, the MarkdownTransformer service
  • 3:11
    isn't actually transforming any markdown, so let's fix that.

Create a Service Class in Symfony 3


In this lesson, we'll move code out of the Controller and into a new class. You'll learn how to create this new service class and refactor the code that you took out of the Controller to work in this new context.

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.