Creating a Reusable (& Amazing) Symfony Bundle


What you'll be learning

Want to share some code between projects, or maybe with the whole world? Let's do it! By creating a Symfony bundle! In this tutorial, we'll learn about bundles, their super-powers, how to add services & routes and the best-practices to create the best bundle possible:

  • Anatomy of a Bundle
  • Bundles vs Libraries
  • Building a bundle inside your app
  • DependencyExtensions to add Services
  • Complex Service configuration: Definitions & compiler passes
  • Allowing config via a Configuration class
  • Private services
  • Routing & other Configuration
  • Creating a Recipe
  • Registering your bundle with Packagist
  • Handling Releases

Not only will you be able to create your own bundle, but we'll learn a lot along the way about how all bundles in the Symfony world work!

Your Guides
Ryan Weaver

Buy Access

Questions? Conversation?

  • 2019-09-09 Vladimir Sadicov

    Hey Fire01

    Thank you for your feedback! We are very happy that you like it! Stay on tune!


  • 2019-09-08 Fire01

    This is great tutorial for reusable my code, thanks...

  • 2019-08-06 Victor Bocharsky

    Hey Virginie,

    No problem! Thank you for your feedback! Glad it was useful for you ;)


  • 2019-08-06 Virginie Burlot

    Hi Victor,
    Yeah, I worked a lot with SF since this comment, and I must say, it was kinda stupid.
    Thank you for answering me at that dark time. I now understand what this course is about and its usefulness. :)

  • 2019-04-08 Steve Johnson

    Thanks! That worked perfect.

  • 2019-04-05 Vladimir Sadicov

    hey @Steve Johnson

    I think you should look at this link it will be better to use this method to modify twig configuration from your custom bundle!


  • 2019-04-04 Steve Johnson

    What method should be used to load global twig variables from a 4.2 symfony bundle?

    I would like to add some twig globals to my bundle using src/Resources/config/twig.yaml.

    Here is what my twig.yml file looks like:

    my_name: 'Jane Doe'

    In my DependencyInjection Extension file I include:

    $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

    I get the following error:

    There is no extension able to load the configuration for "twig" (in src/DependencyInjection/../Resources/config/twig.yaml).
    Looked for namespace "twig", found none

    I try from my services.yaml file

    - { resource: twig.yaml }


    I get the same error.


  • 2018-11-20 weaverryan

    Hey Vinoth Kumar S!

    Ah, an excellent topic! And one that has a very simple answer:

    1) Put you template files in a Resources/views directory in your bundle - e.g. Resources/views/admin/index.html.twig
    2) When rendering, refer to them with a special "@BundleName" syntax... where BundleName is the name of your bundle, but without the last word Bundle. For example, if you want to render the above template from your SymfonyCastsHelloWorldBundle, you would use:

    return $this->render('@SymfonyCastsHelloWorld/admin/index.html.twig');

    That will work, and you will be able to override any templates in your project by following the normal "override mechanism" -

    I hope that helps!


  • 2018-11-19 Vinoth Kumar S

    Hi! This tutorial is awesome to quickly setting up a custom bundle. It will be more helpful if you explained something about twig file rendering from the custom bundle.

  • 2018-04-12 weaverryan

    Hey Rafael!

    Bah! Unfortunately, that's the one thing that I did *not* cover in this tutorial.... in part because it's super tricky to record properly. But if you have any questions, let us know. My advice is this: your bundle may NOT need a recipe at all - many do not. You only need a recipe if your bundle requires some configuration or other files (e.g. the Twig recipe creates a templates/ directory). A recipe is basically "automating" the README. So if your README says "Do X, then create file Y", these are great things for the recipe :). And mostly, recipes are quite simple: you include files that you would like copied into the user's project.


  • 2018-04-07 Rafael

    Hey, Is it going to cover how to create and publish sf flex recipe for this custom bundle? it would be nice!

  • 2018-04-06 Victor Bocharsky

    Hi Virginie,

    Really? I could find this course by "Symfony Bundle" keyword, see . I wonder what keywords you searched for. Well, yes, Symfony 4 is bundle-less, but it means you should avoid creating bundles in the src/ folder of your project, but third-party bundles are still valid. And this screencast is not about creating project bundles but third-party bundles. In shorts, you create a bundle only when you need to share some code between projects. And this course probably will be in the Symfony 4 track because, well, we're using Symfony components v4.0 in this course. But most of the information we show in this course is valid for Symfony 3 as well, of course, if we're talking about sharable third-party bundles.


  • 2018-04-06 Virginie Burlot

    Hi !
    I see that this course is currently in progress, and I wonder, will it be implemented into the SF3 or 4 Track ? I know that there are no more bundle into SF4, so, does it concern SF3 exclusively ?
    Also, I must say that I can't find this course into your searching tool. I only found it while going through the "Courses updates" through my profile.