Archived video

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

Alternate resources: 

Bundles in Symfony 3

Video loading...

  • 0:05
    Bundles in Symfony 3 Ryan Weaver
  • 0:08
    Hey, friend.
  • 0:08
    I'm really glad you're here.
  • 0:10
    This is a big episode for us.
  • 0:12
    We're about to learn some of the most critical concepts
  • 0:16
    that will really help you master Symfony.
  • 0:19
    And of course, impress all of your friends.
  • 0:22
    Like always, you should totally code along with me.
  • 0:25
    Download the code from the course page
  • 0:27
    and move into the start directory.
  • 0:30
    I already have that, so let's start the built-in web server.
  • 0:33
    Open a new terminal tab
  • 0:34
    and run the ./bin/console server:run command.
  • 0:38
    Ding!
  • 0:39
    Now, dust off your browser
  • 0:41
    and try to load localhost:8000/genus/octopus.
  • 0:47
    That's the page we made in the last tutorial.
  • 0:49
    Awesome!
  • 0:51
    After episode one, we already know a lot.
  • 0:54
    We know that Symfony's pretty simple.
  • 0:56
    Create a route,
  • 0:57
    create a controller function,
  • 0:59
    make sure that controller function
  • 1:00
    returns a Response object,
  • 1:02
    and then go eat a sandwich.
  • 1:04
    So route, controller, response, sandwich.
  • 1:08
    And suddenly, you know half of Symfony.
  • 1:11
    And you're not really hungry anymore.
  • 1:14
    The second half of Symfony is all about
  • 1:16
    the huge number of optional useful objects
  • 1:20
    that can help you get your work done.
  • 1:22
    For example, there's a Logger object,
  • 1:25
    a Mailer object, and a Templating object
  • 1:28
    that renders templates.
  • 1:32
    In fact, the this->render shortcut
  • 1:35
    we've been using in the controller
  • 1:37
    is just a shortcut to go to the Templating object
  • 1:40
    and call a method on it.
  • 1:42
    All of these useful objects, or services,
  • 1:46
    are put into one big, beautiful object called the Container.
  • 1:52
    If I give you the Container,
  • 1:53
    then you're incredibly dangerous.
  • 1:56
    You can fetch any object you want and do anything.
  • 2:00
    How do we know what handy services
  • 2:01
    are inside of the Container?
  • 2:03
    Just use the debug:container command.
  • 2:07
    ./bin/console debug:container
  • 2:12
    You can even search for services, like log.
  • 2:16
    But where do these come from?
  • 2:18
    What magical, mystical creatures
  • 2:21
    are providing us with all of these free tools?
  • 2:24
    The answer is, the bundle fairies.
  • 2:28
    I mean, just the bundles.
  • 2:31
    In your IDE, open up app/AppKernel.php.
  • 2:35
    The Kernel is the heart of your Symfony application.
  • 2:38
    But it really doesn't do much.
  • 2:41
    Its main job is to initialize all the bundles we need.
  • 2:45
    A bundle's basically just a Symfony plugin.
  • 2:47
    And its main job is to add services to your container.
  • 2:52
    Remember that giant list from a minute ago?
  • 2:54
    Yep, every single service in that list
  • 2:57
    is provided to us from one of these bundles.
  • 3:02
    But at its simplest,
  • 3:03
    a bundle is basically just a directory
  • 3:06
    full of PHP classes, configuration,
  • 3:09
    and other goodies.
  • 3:11
    And hey, we have our own, AppBundle.
  • 3:17
    OK, I have a challenge for us.
  • 3:19
    I want to render some of this octopus information
  • 3:22
    through a markdown parser.
  • 3:25
    So the question is,
  • 3:26
    does Symfony already have a markdown parsing service?
  • 3:30
    I don't know.
  • 3:31
    So let's find out via debug:container, pass it markdown.
  • 3:37
    Hmm, nothing.
  • 3:39
    So there's no built-in tool to help us.
  • 3:42
    Symfony community to the rescue!
  • 3:44
    If you're missing a tool,
  • 3:45
    there might be a Symfony bundle that provides it.
  • 3:49
    In this case, there is.
  • 3:50
    It's called KnpMarkdownBundle.
  • 3:56
    Copy its composer require line.
  • 3:58
    You don't need to include the version constraint.
  • 4:01
    Composer will figure that out for us.
  • 4:03
    Run that in your terminal.
  • 4:05
    composer require knplabs/knp-markdown-bundle
  • 4:11
    Let's keep busy while that's working.
  • 4:13
    To enable the bundle,
  • 4:15
    grab the new statement from the docs
  • 4:17
    and paste that into AppKernel.
  • 4:19
    The order of these, by the way, does not matter.
  • 4:23
    And that's it.
  • 4:24
    Just wait for Composer to finish its job.
  • 4:26
    And maybe send a nice tweet to Jordi.
  • 4:29
    He's the creator and maintainer of Composer.
  • 4:33
    Now, before we do anything else,
  • 4:36
    let's run an experiment.
  • 4:39
    Try running debug:container again
  • 4:43
    with a search for markdown.
  • 4:45
    Boom!
  • 4:46
    Suddenly there are 2 services matching.
  • 4:49
    These are coming from the bundle we just installed.
  • 4:52
    The one we're really interested in is markdown.parser.

Bundles in Symfony 3

Loading...

In this lesson, you'll learn more about the fundamentals of Symfony 3 and how to get access to more services for your app through installing bundles. If you haven't built your first app in Symfony 3 yet, head over to Joyful Development with Symfony to get up and running, then head back to this series and continue your Symfony 3 journey!

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.