> Symfony 4 >

Course Overview

Login to bookmark this course

Symfony 4 Fundamentals: Services, Config & Environments

Unlock Symfony 4's magic: learn how to find/use services, dive into Symfony Environments & configuration loading!

  • 5992 students
  • EN Captions
  • EN Script
  • Certificate of Completion

Your Guides

About this course

What PHP libraries does this tutorial use?

// composer.json
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
        "nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
        "php-http/guzzle6-adapter": "^1.1", // v1.1.1
        "sensio/framework-extra-bundle": "^5.1", // v5.1.4
        "symfony/asset": "^4.0", // v4.0.4
        "symfony/console": "^4.0", // v4.0.14
        "symfony/flex": "^1.0", // v1.21.6
        "symfony/framework-bundle": "^4.0", // v4.0.14
        "symfony/lts": "^4@dev", // dev-master
        "symfony/twig-bundle": "^4.0", // v4.0.4
        "symfony/web-server-bundle": "^4.0", // v4.0.4
        "symfony/yaml": "^4.0" // v4.0.14
    "require-dev": {
        "easycorp/easy-log-handler": "^1.0.2", // v1.0.4
        "symfony/debug-bundle": "^3.3|^4.0", // v4.0.4
        "symfony/dotenv": "^4.0", // v4.0.14
        "symfony/maker-bundle": "^1.0", // v1.0.2
        "symfony/monolog-bundle": "^3.0", // v3.1.2
        "symfony/phpunit-bridge": "^3.3|^4.0", // v4.0.4
        "symfony/stopwatch": "^3.3|^4.0", // v4.0.4
        "symfony/var-dumper": "^3.3|^4.0", // v4.0.4
        "symfony/web-profiler-bundle": "^3.3|^4.0" // v4.0.4

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!

Next courses in the Symfony 4: Blast Off! (beginner) section of the Symfony 4 Track!


Sort By
Login or Register to join the conversation
Default user avatar Rafael 6 years ago

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

4 | Reply |
JuanLuisGarciaBorrego avatar JuanLuisGarciaBorrego Rafael 6 years ago

I join the request =)

14 | Reply |

Hey guys,
Still, there's lots of mysterious things about extracting the bundle and push into packagist.org, 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 :)

4 | Reply |

Hey ahmedbhs

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 :)

1 | Reply |

Hey Ahmed,

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


1 | Reply |

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: https://github.com/knpunive....

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.


1 | Reply |


| Reply |

Hey Rafael

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


| Reply |
Kribo avatar Kribo 6 years ago

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

1 | Reply |

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.


| Reply |
Kribo avatar Kribo Victor 6 years ago edited

victor 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....

| Reply |

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: https://github.com/symfony/... . 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: https://symfony.com/doc/cur... - 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


| Reply |
Kribo avatar Kribo Victor 6 years ago edited

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...

| Reply |

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: http://fabien.potencier.org... 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: https://symfony.com/blog/th...

> 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.


| Reply |
Kribo avatar Kribo Victor 6 years ago edited

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 teach....lol

#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 cookies...lol

#4 I finally signed up to "Medium.com"...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 upload...lol

| Reply |

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 https://github.com/symfony/... - 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: https://knpuniversity.com/s... - it will help you a bit, probably only a few BC breaks are introduced in Symfony 4.


| Reply |
Krzysztof-M avatar Krzysztof-M 1 year ago

By default, I have all services private by default in the container. I would like to temporarily fetch services directly from the container, not by injecting in the constructor. Temporarily, i.e. after downloading the service instance - such a service would remain private in the container (only for the time of downloading it would become public).
Is there such a thing to achieve? If so, I'd appreciate any pointers on how to do this.

| Reply |

Hey @Krzysztof-M ,

I think you are contradicting yourself :) First of all, I'm not sure what do you mean about downloading ) I suppose it's the same as get the service from the container. But to do so, the service should be public but you want to keep it private. It seems like you want to have the same service public and private at the same time ) Anyway, that's not something that is possible, unless you're talking about a testing environment, where all your private services still can be fetched from the container for testing purposes.

But for prod/dev env - I think you need to look into the direction of service subscribers: https://symfony.com/doc/current/service_container/service_subscribers_locators.html - you can create a small container for specific services, inject it instead, and fetch whatever private services you want from it (but you would need to declare to which services that container will have access).


| Reply |
Krzysztof-M avatar Krzysztof-M Victor 1 year ago edited

Thannks for answer @Victor

What do you think about getting services by following the schema:

         $a = clone $container->get(MyClass::class);

Problem is that I don't know how to get ContainerBuilder?

| Reply |

Hey @Krzysztof-M ,

I really don't see a point in it. What are the benefits of doing this? IMO you're just mixing concepts, trying to make your services both private and public at the same time which will lead to confusion. I would not recommend doing this, instead, take a look at the Symfony best practices with service subscribers and locators I linked above.

But if you see some benefits for your with this behavior, ok then. I think you can leverage bin/console debug:container to find it out. It also has some useful options and flags, add --help to see them all. But I think ContainerBuilder is something that available only during the building the container, not after it's already built.


| Reply |
Hotspot 3 avatar Hotspot 3 5 years ago

Hi guys, thanks for the wonderful tutorials.
There is something I am trying to archieve but I really don't have a clue on how to do it, I hope you can guide me :)

Making it simple, in this example:

1) I have three entities: "IssuingEntity", "LegalEntity" and "ComercialEntity". One IssuingEntity can have many LegalEntities and a LegalEntity can only have one IssuingEntity (OneToMany). And one LegalEntity can have many ComercialEntities and one ComercialEntity can only have one LegalEntity.

2) There is also "Invoice". One invoice has only one ComercialEntity, a ComercialEntity can have many Invoices.

3) I generate an Invoice for ComercialEntity "A" for this month.

4) Next month I update the details of the ComercialEntity and generate a new invoice for this month for ComercialEntity "A".

The idea is to be able to see the invoices and the comercialentity data in a "histogram" way. Like what was the data in february?, then jump to june...

How can I travel back and forth in time and be able to see the state of the invoice, comercial entity or any other? I mean, if i check month in point 3) I should see the details of the comercial entity by that time, and if I check the month in step 4) I should see the details by that time.

The idea is like taking "snapshots" of the doctrine by months and be able to go back and forth. Not sure if I am clear explaining...

I was thinking maybe Doctrine Unit of work, migrations or maybe a hand made system like storing start and end dates for each entity and each time an entity is updated, set end date and create new lines, but this would be a hell of a hard work...

Please how can I manage this?
Thanks in advance

| Reply |

Hey Jacobo,

First of all, if you need a history of invoices, i.e. you need to save each invoice in the DB so you should avoid overriding data, instead create a new invoice for the next month instead of changing/updating it. Otherwise it's too complex to make some kind of diffs system between invoices, and I bet you don't need this complexity. So, if you will have a list of timestamped invoices, you will be able to go back and forth in time and see who the invoice looks like in the February, etc. - you just need to write a proper query to fetch invoices by specific date.

Btw, try to generate relations with MakerBundle, i.e. bin/console make:entity command. This command can also update entities, so you don't need to remove and re-create already existent ones. For the field type input "relation" and follow the tips of this command, it helps you to clearly understand what exactly relations do you need by explaining each possible relations.

Btw, we're talking about this command here: https://knpuniversity.com/s...

I hope this helps!


| Reply |

Hi Victor, thanks for the reply.

In my system I already have timestamped invoices by month, the problem is, that some of the fields in the invoice are relations, for example, the ComercialEntity. So, let's say on august I generate an invoice with ComercialEntity A, and then in september I change the name of that ComercialEntity to B. Then in september I generate another invoice. The Id of the ComercialEntity will be the same yes, but the name not. So, if in this moment i go look the invoice of august, the name will be B and not A.

The only solution I found is to store the "visual" content of the invoice, the html, in a new field and only update it when the invoice is generated. Not sure if this is the proper way...

Thanks a lot!

| Reply |

Hey Gst I.

In that case where your related entities may change its data and you rely on them, then you have to store the value that they had at that specific time. I wouldn't store the whole HTML but only the fields of the related entities that I'm printing


| Reply |

Thank you Diego!

I will give it a try


| Reply |
Default user avatar Lekan Olaleye 5 years ago

Hi. Please I'd like to know if you have a tutorial that covers deploying Symfony 4 on a shared hosting platform using FTP

| Reply |

Hey Lekan,

We do have a screencast about deploying Symfony with Ansistrano: https://knpuniversity.com/s... - but you need to have an SSH access to the server in order to follow it. Some shared hosting platforms may have SSH access with limited rights, i.e. you have access to the server in console, but you don't have access to install things etc.

If you only have FTP access - you won't have zero downtime deploys, i.e. your customers will see a broken website during some time when your deploy is in progress. That's why deploying Symfony to shared hosting is not recommended, and that's why we won't have a screencast about it - we don't want to teach bad practice.

So, the easiest solution for you is just drag-and-drop the files on prod server using an FTP client like FileZilla. I think you can even write a custom shell script to upload the files via FTP. But better try to find a hosting with ssh access.


| Reply |
Cameron avatar Cameron 6 years ago

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!

| Reply |

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...

| Reply |

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: https://knpuniversity.com/s... . 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.


| Reply |

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.

| Reply |

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.

| Reply |

Hey Fox C.

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"


| Reply |

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": "https://github.com/camburns...",
"type": "git"
require: {
"camburns/toolsbundle": "dev-master"

| Reply |

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

| Reply |
Default user avatar cybernet2u 6 years ago

No User Feature ?

| Reply |

Hey cybernet2u,

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 ;)


| Reply |
Default user avatar Ivan Ion 6 years ago

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

| Reply |

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


| Reply |
Default user avatar N. Karthic Kannan 6 years ago

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?

| Reply |

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: http://symfony.com/doc/mast... you can read a lot more info about managing

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

| Reply |
Default user avatar N. Karthic Kannan MolloKhan 6 years ago edited


Thank you for your response. However, remember me is not what I look for. I found the actual problem and fixed it successfully. You can read it here https://stackoverflow.com/q...

| Reply |

Nice workaround!
Thanks for sharing your solution

Have a nice day :)

| Reply |

Delete comment?

Share this comment

astronaut with balloons in space

"Houston: no signs of life"
Start the conversation!