Symfony 4 Fundamentals: Services, Config & Environments

Buy Access

Ready to take your Symfony skills to the next level? Then let's talk about what's at the core of Symfony: services and the container. In this tutorial, you'll learn about what makes your app "tick", including:

  • Installing external packages
  • Finding and using services
  • Configuring bundles (i.e. configuring outside services)
  • Symfony Environments
  • The magical configuration loading mechanism
  • Service parameters
  • Environment Variables and .env
  • Bonus: The MakerBundle

By the end, you'll see how your app works under-the-hood and understand the fundamentals behind any feature in Symfony. Let's go!

Your Guides

Ryan Weaver

Questions? Conversation?

  • 2018-03-16 Diego Aguiar

    Hey N. Karthic Kannan

    You need to activate the "remember_me" functionality, and then you can set up a very long "lifetime" value.
    This is not actually what you want, it will expire at some point, but maybe is good enough?
    Here: you can read a lot more info about managing

    I hope I've helped you even for a bit.

  • 2018-03-16 N. Karthic Kannan

    Is it possible to retain an indefinite login session in a Symfony application? i.e; the logged in user should not be logged out automatically after a certain period of time.

  • 2018-03-16 N. Karthic Kannan

    My Symfony application is automatically logging out after some specific time. How can alter it to retain the session till the user logs out from the system by clicking the logout button?

  • 2018-03-15 Diego Aguiar

    Hey Ivan Ion

    Haha, we would love to be oficially named as the "Gods of PHP"! :D
    Thanks for your suggestion, we *do* consider all suggestions

    By the way, we have a tutorial about ReactJS + Symfony4 in a planning stage, just in case you are interested


  • 2018-03-15 Ivan Ion

    Hey guys! I am expecting from you a tutorial about apiplatform+reactjs and you will became The God of PHP!

  • 2018-03-01 Diego Aguiar

    Hmm, that's weird, I know many structural things have changed in Symfony4, but you should be able to overcome this problem regardless of your Symfony's version. Can you double check that the bundle is installed? I mean check that the bundle's files are inside your vendor directory, or you know what, you could delete your vendor directory and re-install everything with composer

    Btw. I couldn't see the "autoload" section of your composer.json file

  • 2018-03-01 Cameron Burns

    yes, the bundle is used in prod also, my bundles.php file includes:
    VisageFour\Bundle\ToolsBundle\ToolsBundle::class => ['dev' => true, 'test' => true, 'prod' => true]

    php bin/console cache:clear

    produces the error:
    Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "ToolsBundle" from namespace "VisageFour\Bundle\ToolsBundle".

    is it better to just use symfony 3.4? There's other issues such as bundle inheritance that no longer exists in SF4 and I'm not having much luck finding any useful information on these important changes. I'm rewatching the SF4 videos but I can't recall the training material covering any of these topics.

    composer.json is:

    "repositories": [
    "url": "",
    "type": "git"
    require: {
    "camburns/toolsbundle": "dev-master"

  • 2018-02-28 Diego Aguiar

    Hey Cameron Burns

    Did you require that bundle for all environments?
    How's your "autoload" configured in your composer.json file of your private bundle?
    And, just in case, try clearing the cache, but this time removing everything inside "var/cache"


  • 2018-02-28 Cameron Burns


  • 2018-02-28 Cameron Burns

    I used composer init (thanks for this btw!) and it recognised the package and downloaded it via composer, however I've getting this error:
    Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "ToolsBundle" from namespace "VisageFour\Bundle\ToolsBundle".
    Did you forget a "use" statement for another namespace? in /Library/WebServer/Documents/twenchaFF/src/Kernel.php:32

    I added the bundle to bundles.php but no luck.

    (phpstorm can identify and link to the class so I assume it's not a namespace typ-O).

    If you have any suggestions, I'd be happy to try! if not, I'll roll back to 3.x, I feel like this issue should have been addressed more by symfony - many changes are self-evident, I feel like quite a few people will struggle with this one.

  • 2018-02-28 Cameron Burns

    Cheers victor, this to me seems like a really bad idea . from symfony - how are we supposed to edit private bundles on the fly? Previously this was done via submodule and this system worked well, now we need to edit the vendor bundle that isn't git enabled? Or should we setup a submodule git repo in the vendors folder?

    Would have been great to see a video on this issue, I thought would be a common issue devs would face.

  • 2018-02-28 Victor Bocharsky

    Hey Cameron,

    Probably the best way is to move this bundle to a separate repository, then do "composer init" which will create a composer.json file, add this bundle as your project dependency with "composer require your/bundle-name", and allow Composer to download it to the vendor/ dir on "composer install".

    Or, you can keep storing it inside src/ bundle but probably you need a separate autoload rule for it, see this chapter where we load legacy AppBundle from src/ dir: . Also, don't forget to exclude this bundle from registering all its files as a services in your config/services.yaml - you probably don't need it.


  • 2018-02-28 Cameron Burns

    I found this article:

    But not only does it not appear to be able to load the custom bundle from composer but in the comments, it's also mentioned that it's a bad practice.

    I've also tried directly download from github via zip and putting directly into src and vendor - this doesn't work either.

    very strange...

  • 2018-02-28 Cameron Burns

    Hi Ryan,

    Question on loading custom bundles:
    I have a utilities bundle that does useful things like extend controllers and provide utility services that I previously managed via GIT submodule in symfony 2.8. This also worked when deploying to heroku - the submodules were automatically downloaded when building a slug and the utilities bundle was stored in src.

    However the folder structure has changed a bit in SF4 and it doesn't look like putting bundles into source would be to convention, maybe this would be loading via composer?

    Keen to hear your thoughts - wasn't able to find anything in the videos about this, did I miss something?

    BTW, been enjoying watching these clips - thanks once again!

  • 2018-02-19 Victor Bocharsky

    Hey kribo ,

    Haha, no problem :)

    Yeah, I know, it's difficult to read a lot of docs at once.

    #3 Hm, it made more sense for Symfony 3, and we really had a tool which helps with it. Probably it makes less sense with Symfony 4 since now you need to create a bundle only when you want to share your code. But I think it'd would be useful to have such tool - it'd prevent simple misprints and save some time. Well, you can track the discussion in - probably maker bundle is a good place for it.

    #5 Yes, we do have such plans. Unfortunately, this course is going to be started after a few more courses. The reason is that we need to release "Database & Doctrine" and "Symfony Forms" courses before we start working on "Security, Registration and Authentication with Guard" one. Well, if you need this course now - we have a similar one but in Symfony 3 track, you can check it out here: - it will help you a bit, probably only a few BC breaks are introduced in Symfony 4.


  • 2018-02-16 kribo

    Victor Bocharsky
    Hi there.
    Yip he's back....
    First off Sorry about the wining, didn't mean to wine...
    And nope have not dumped S4........(yet).....

    #2 Once again some awe-some links and again lots to read...
    My brain is throbbing with idea's and questions...time to experiment with S4.. Tha's a course I should

    #3 I get why S4 is bundless but I do wish that "maker" gets an addon "bundle"
    may be not bundle but more "snippet"
    -a bundle is code that can exist on it's own
    -a snippet could be for grouping of code that cannot live on its own but that you repeat everytime when creating new projects.
    am I over-engineering it again?

    To much sugar or space

    #4 I finally signed up to ""...yip time to bombard other authors with questions.

    #5 I tried to implement GUARD into S4 and it crashed nothing new there..
    But do you have S4 and Guard planned as a course?

    thnx alot more questions to follow upon next

  • 2018-02-16 Victor Bocharsky

    Hey kribo ,

    Haha, yes, that's not a one question :)

    Yeah, I see your point and I'm sorry you're upset, but Symfony 4 along with Flex is still a pretty new and unique concept, and since it's new and fresh - of course it may have some lack of information. Actually, Symfony has a lot of docs, but also a lot of docs need to be upgraded, and also the *new* docs should be added. Symfony community do their best with it, you can see the progress it in the symfony/symfony-docs repo, I hope you understand it's not an easy process upgrading docs. Why Symfony goes this way about bundle-less architecture - there're a lot of reasons behind it, well, which also has its pros and cons. I bet Symfony devs has a giant discussion about this concept, unfortunately I don't have a link to it. Also, Fabien has written many articles about the new Symfony 4 and Flex, and during those publications Symfony team actively track feedbacks and even many changes were made thanks to those feedbacks, you can read the new directory structure blog post here: and also look over other related publications, but probably you won't read it either. What I'm trying to say is all those decisions were not done blindly, the big history behind it.

    OK, now about your question:
    > 1-How does one go to work in creating the shareable bundle?
    Both are legitimate ways, but probably with A) you need more setups, and since we're talking about sharing this bundle, so you'll need to do the steps from B) anyway because you want to see this bundle in vendor directory when you want to share some code. So, I'd choose the B) way.

    > 2- Lets say I create a User Mgt bundle which rely's on Symfony/Security to be installed
    I'm not sure what is "User Mgt", but definitely, if your code in the bundle relies on symfony/security component - you need to add it as a dependency to the bundle's composer.json. Well, you can totally ignore creating any recipes for your bundles unless you understand you really need them. Please, skip it at least on the first steps pf creating the bundle. You can think about it when your bundle more or less becomes stable. Flex will help to register your bundle automatically, for any other additional configuration - just write some instructions in README, i.e. just write good docs. And btw, recipe vs adding dependencies in composer.json are totally different things.

    > 3- If -2- is use a recipe?
    >A) Do I need to create a second git just for the manifest recipe?
    Yes, you did. Well, that's not actually true, you need to submit a PR to the symfony/recipes-contrib repo, read the links in my previous comment. But once again, better forget about recipes on the initial stage and just write a good docs about how to use your bundle - it's much better for new, unstable and "not popular too much yet" bundles.

    >B) Can the manifest and composer.json file live in the same bundle git?
    Nope, at least I don't know it's possible, and probably it's NOT possible if we're talking about symfony/recipes-contrib repo :) And manifest vs composer.json - are totally different things.

    > I really don't understand why the symfony community makes it so difficult and expensive for newbees to learn.
    Well, we're here for help newbees (and not only newbees) with our screencasts. With online videos we can deliver good and quality content a lot of people at once. In case of trainings in person - it's impossible to gather 1000, or even 100 people in one place and at one time together and part of those money goes to trainer, part to rent a room, etc. so it's tough question. But what I'm trying to say is that it's possible to make this price cheaper teaching people online what KnpU is trying to do.

    > S4 is a bundle but everybody must go bundless?
    So, yes, Symfony components are used by million applications but... your application probably is used only by you, right? So why make your private application as a bundle if nobody can use it? You mess up component vs application - those are totally different things.

    > Is S4 throttling the community? And by throttling I mean choking them into doing it there way?
    Probably it may seem so to you (and I hope it won't after you read this comment) but that's not true. All those steps were done more or less by the community, and Symfony always trying to deliver all the best to its users. And in Symfony 4 this way was chosen as the best way. Even the reverse, Symfony want to be more friendly for newbees and start making steps in this direction, see the Diversity Initiative blog post:

    > Or is it just me due to the fact I'm struggling into finding answers and google, bing and whatever are whorthless always the same crappy pages or Symfony 2 video's...
    Once again, Symfony 4 is so young, only a few months, so of course you'll find more information about Symfony 2/3 on the internet than about Symfony 4, and that makes sense. As you can see, we're doing all our best to make more Symfony 4 related screencasts releasing new videos every day, but of course, we can't jump above our heads.

    I hope my comment helps you with your questions.


  • 2018-02-15 kribo

    Victor Bocharsky
    Thnx for the links, I've one question, which isn't true but lets start @#1...

    -1-How does one go to work in creating the shareable bundle?
    A) I create a new symfony/skeleton project and in the src/AcmeBundle
    and put within this folder a new Composer.init project file
    B) I create a composer.init project and then upload it to git and pull my new bundle into vendor/ of a new/existing symfony project.

    -2- Lets say I create a User Mgt bundle which rely's on Symfony/Security to be installed
    A) Do I create a manifest recipe
    B) Do I add "Symfony/Security" to the composer.json file of the bundle -> require{}

    -3- If -2- is use a recipe?
    A) Do I need to create a second git just for the manifest recipe?
    B) Can the manifest and composer.json file live in the same bundle git?
    and if so which one gets called first?

    As you can see there are a lot of topics to create courses on.
    I really don't understand why the symfony community makes it so difficult and expensive for newbees to learn.
    As if a student can pay for a 6day course from sensio and pay 2000$.
    I dont know...
    This is what I see...
    First Symfony costs an arm and a leg to learn in hours and fortune.
    Second Symfony 4 removes bundle => removes modular possibility => and the only people who know how to bundle in S4 are the elte.
    => which is a bit hippocratic no? => S4 is a bundle but everybody must go bundless????

    Is S4 throttling the community ??
    And by throttling I mean choking them into doing it there way?
    Or is it just me due to the fact I'm struggling into finding answers and google, bing and whatever are whorthless always the same crappy pages or Symfony 2 video's...
    Is Symfony worth it? Did I make the right choice? Maybe it's time to pull the plug and dump S4? Goto Laravel maybe?
    This just reminds me of my days when programming for Micrsoft Dynmics NAV...You cannot find shit on the internet on howto??
    Dejavu all over again...

  • 2018-02-15 Victor Bocharsky

    Hey kribo ,

    Ah, OK. Let ,e clarify some information for you. Symfony is mean to be bundle-less, i.e. you should avoid creating bundles in your application, just put everything in src/ dir. But bundles still make sense for sharing code between projects, so if you have a few projects and you want to share some code between them - you need a bundle. Or you need a bundle if you want to share your solution with other users and you think it could be useful for someone. You're right, "generate:bundle" command is gone, it was replaced with Symfony MakerBundle, but it does not have a way to generate bundles yet, though there's a proposal about it, so you can track it if you're interested: . But for now, you need to create a bundle manually or use the older version of Symfony 3.x where you're able to use "generate:bundle" command. Well, create it manually is not too difficult, you always can steal some skeleton from plenty of open source bundles, any you like. To make things a bit clear, you can check this docs: - which is described some bundle structure. That's it for now. We'd like to make a tutorial about making bundles, unfortunately this one in plans for the future, not for now.

    I hope this helps you


  • 2018-02-14 kribo

    Victor Bocharsky Hi,
    As symfony 4 my first symfony is I don't know how it was done in 3.X except that there was a generate:bundle command. So watching video's on youtube is becoming a pain because it all comes down to generate:bundle.
    But what I don't understand is that symfony wants to bundless but all that symfony is is a collection of bundles. So is knp, sonata etc. Are they making bundles for symfony 4 by using symfony 3. All I wish to know is how to stop me from making repetitive work. Every new project = a new userbundle, securitybundle, productbundle, customerbundle, vendorbundle etc....

  • 2018-02-14 Victor Bocharsky

    Hey kribo ,

    We have this idea about creating bundles in mind, but it's not a tutorial we're going to start right now, so I can't say any estimations, thanks for understanding. But yeah, having this in the future would be cool! But I can tell you the process is the same as for Symfony 3.x, so you can steal the skeleton from any public Symfony bundle and try to tweak it for yourself.


  • 2018-02-13 kribo

    So The big question how do you create a shareable bundle in symfony 4 ?

  • 2018-02-06 Juan Luis Garcia

    I join the request =)

  • 2018-02-01 Victor Bocharsky

    Hey Ahmed,

    Btw, we have an article about creating a package on Composer, you probably may be interested in it, check it out here: . Yes, it's not about creating a *bundle*, but may help a bit with the process.


  • 2018-01-31 Diego Aguiar

    Hey Ahmed Bhs

    Thanks for letting us know your interest over that topic, look's like we will cover it in a further tutorial, but I can't give you any estimate of a release date yet

    Have a nice day :)

  • 2018-01-29 Ahmed Bhs

    Hey guys,
    Still, there's lots of mysterious things about extracting the bundle and push into, creating the yaml configuration, create some command lines, versioning and adaptability,..
    It will be a great idea to create a course or to publish an article about the steps of creating a reusable bundle :)

    Cheers :)

  • 2018-01-29 weaverryan

    Hey Rafael!

    I'll add one more thing: if you're creating a re-usable bundle, then nothing has changed in Symfony 4 :). The directory structure is still the same and the best-practices are still the same. We *are* recommending that bundles make more private services versus public services. And in fact, in Symfony 4, you need to explicitly set a service to be public in order for it to be public (where in Symfony 3 and before, public was the default). Also, you should add autowiring aliases so people can autowire your services - here's an example:

    So, there are a few minor things... but nothing major has changed. We've mostly changed recommendations for application code to make people's lives easier and more fun :p. I'd be happy to answer any other specific questions.


  • 2018-01-25 Diego Aguiar

    Hey Rafael

    At the moment we don't have a course covering that topic, but we will take your comment as a tutorial suggestion :)


  • 2018-01-24 Rafael

    Is there any course which covers how to build reusable "bundle" in the new symfony 4 style?

  • 2018-01-23 Victor Bocharsky

    Hey AdFlorin,

    Unfortunately, no user feature yet. Actually, we're going to add this part after DB and forms topics, i.e. almost at the end and that makes sense because user feature requires both DB and forms ;)


  • 2018-01-23 AdFlorin

    No User Feature ?