Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Symfony 3 Fundamentals: Bundles, Configuration & Environments

41:30

What you'll be learning

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.22
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "knplabs/knp-markdown-bundle": "^1.4" // 1.4.2
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.7
        "symfony/phpunit-bridge": "^3.0" // v3.1.3
    }
}

After the first course - Joyful Development with Symfony3, you've got Symfony up and running. CELEBRATION!

In this episode, it's time to get down to work by learning the fundamental concepts of bundles, configuration and environments! We'll discover what all the files in a Symfony project do and why, the purpose of bundles and the power behind environments. So let's get to work: what we're about to learn will pay dividends for us in the future:

  • Where do services come from?
  • Installing an external bundle
  • Configuring bundles
  • Controller outside services
  • Using config:dump-reference
  • Understanding Symfony environments
  • Create your own environment
  • The prod environment and cache:clear
  • The famous parameters.yml
  • Where do routes come from?
  • src/ versus app

Ready, set, go!


Your Guides

Ryan Weaver Leanna Pelham

Buy Access

Join the Conversation?

42
Login or Register to join the conversation
Default user avatar
Default user avatar Constantin | posted 4 years ago

Hey authors! Thanks for this great learning resource. I am migrating from Laravel to Symfony and the way you structured the course makes it a piece of cake. Thanks!

4 Reply

Dude, awesome! Welcome :)

Reply
Default user avatar
Default user avatar Paul De Zwaan | posted 4 years ago

Authors!, symfony is not disappointing me sofar , before have been doing a lot of stuff in Yii, as i have been wanting to try out symfony for a long time, this was really the help that i needed to switch from framework. So big Thanks! Keep up the good work!

1 Reply
Default user avatar
Default user avatar Simon Tran | posted 4 years ago

Your courses is amazing, thank you it all !

1 Reply
Default user avatar

My favorite Symfony IDE is Codelobster

Reply
Christopher M. Avatar
Christopher M. Avatar Christopher M. | posted 3 years ago

How relatable is this with Symfony 2.6?

Reply

Hey Christopher M.!

Hmm, still pretty relatable, but not entirely, of course. The biggest change would be the directory structure - it's a bit different between Symfony 2.6 and 3. However, all the fundamentals of service configuration, bundle configuration, parameters, environments, etc are all pretty much identical between the versions -I think this would serve you well. And if you have questions, you can always ask us :).

Cheers!

Reply

WOW! Thanks Ryan & Leanna!

Reply
Default user avatar
Default user avatar Sams Raina | posted 4 years ago

Hi Team,
Can you share security context tutorial for symfony 2 ?

Reply

Hey Sams,

Could you explain a bit more what you mean about "security context tutorial"? Did you see our Symfony Security tutorial? Is it something you're looking for but for Symfony 2 only?

Cheers!

Reply
Default user avatar
Default user avatar Shairyar Baig | posted 4 years ago

I was looking for a way to allow the admin of a symfony based app to upload a logo via admin panel and then use that logo on every page, I certainly do not want to ping the database on every route just to get the logo. I cannot seem to find any example on how to achieve this. Any tips :)

Reply

Hey Shairyar Baig!

Caching! There are 2 ways to cache in Symfony: (A) via the fairly new Symfony cache component or (B) via HTTP caching. Honestly, we need a tutorial on both of these. I would use Symfony's new cache component. Let me give you a few details:

A) I don't know what your database setup looks like, but somewhere in your template, you'll need to fetch the image. Create a new Twig function to do this - so something like:


<img src="{{ get_company_logo() }}"/>

B) In your Twig extension, you'll be able to make a query to find this path. Symfony 3.1 also comes with a new service whose id is cache.app, which you can use to cache the path to the logo. Here are some quick docs about it: http://symfony.com/blog/new...

C) This should be not too hard - just use dependency injection to pass that service to your Twig extension - I know you, so I know you can dig into it ;). But, now what happens when your admin uploads a new logo!? We need to invalidate the cache. Basically, when the user uploads a new image (e.g. you could do this in the controller), you'll need to invalidate the cache you just stored. Details here: http://symfony.com/doc/curr...

And of course, I also need to say "Don't prematurely optimize". Having an extra database query is not ideal... but probably doesn't hurt performance much. So, balance that with the added complexity of caching!

Cheers!

Reply
Default user avatar
Default user avatar Shairyar Baig | weaverryan | posted 4 years ago | edited

Hi weaverryan many thanks for getting back with your suggestions. I ended up using rendering controller inside twig https://knpuniversity.com/s... which worked out fine, its not a heavy traffic site so an addition ping to db wont hurt the performance. Thanks.

Reply

Hey Shairyar Baig

I would suggest you to first focus on functionality, and then, when you detect a bottleneck, optimize your code / cache a few queries

Have a nice day!

1 Reply
Default user avatar
Default user avatar Shairyar Baig | MolloKhan | posted 4 years ago

That's exactly what I am doing now :) features are ready just few tweaks here and there. Cache is good idea I will implement that thanks for the tips.

Reply
Default user avatar
Default user avatar Elbarko Lari | posted 4 years ago

i really enjoyed the first tutorial :p i really want to continu if i had some money for that , any way you are awesome

Reply

Hey Elbarko,

Thank you! We'll really miss you too! ;)

Cheers!

Reply
Default user avatar
Default user avatar Thierno Diop | posted 4 years ago

please can u telle me how did you do to have a terminal like yours in your macbook

Reply

Hi Theirno!

Which part - the coloring - or something else? To have a short "prompt" (just the directory at the beginning of each line), I have this code in my ~/.bash_profile file:


export PS1="\w$ "

I also - obviously - have made my font size really big - Manaco 22pt. And I *am* using the stock "Terminal" application.

Does that help? Glad you like it :)

Cheers!

Reply
Default user avatar

thx for the reply i mean the cloring of file and folder and can you be more clear about the export ps1 and how to do it and what you mean by stock"terminal"
THX you did well with these formations

Reply

Ah, I understand! Ok:

1) I'm using the normal "terminal" app in OSX. There are other terminal apps you can download - but this is the normal one that comes with OSX.

2) For the file and directory coloring, I added the following 2 lines to my ~/.bash_profile file


export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

Here is the reference I originally used for this: http://osxdaily.com/2012/02.... After you make the change, open a new terminal tab to see it.

3) If you want to change the text that appears at the beginning of each line in your terminal to look like mine, then you can add the following line to your ~/.bash_profile


export PS1="\w$ "

After you make this, open a new terminal tab to see it. This changes the text at the beginning of each terminal line from:


Ryans-MacBook-Pro-4:oo weaverryan$

to

~/path/to/current/directory$

Cheers!

1 Reply
Default user avatar

thx a lot

Reply
Default user avatar
Default user avatar dominik | posted 4 years ago

Hi Ryan, do you know some tutorial for deploying a Symfony app to a shared hosting, where you are not able to run any console commands? Thanks!

Reply
Default user avatar
Default user avatar Shairyar Baig | dominik | posted 4 years ago

I am running one of the client's project on shared hosting and its no fun :( i miss having access to CLI to run commands specially when i need to clear cache. It will run without problem but you need to setup .htaccess file in root to point to web folder other wise your every URL will have the word web in it like http://example.com/web

Reply

Hi Dominik!

I don't know of any such tutorial. However, the most important thing is that you:

A) Upload all of your files (somehow) to the server
B) Empty the contents of the var/cache directory (app/cache for Symfony 2.x)
C) Make sure the var/cache and var/logs are writeable by the web server (usually done by 777'ing those directories, but this has - in theory - some security implications).

This is a non-standard way to use Symfony, but there's no reason that it shouldn't work.

Good luck!

Reply
Default user avatar

thanks for the reply.
how about the initial setup, like doctrine:database:create and such.. is there some way to do it by a script instead of cli?

Reply

For this, you would dump your local database to an SQL file and import it into your host's database manually (through whatever process they allow you - like phpmyadmin). For database changes in the future, you wouldn't be able to use migrations (like we do in this tutorial) - you would need to make those changes manually (though you could copy the SQL statements from the migration files and execute those manually).

It's not *ideal* to run Symfony without CLI access. It's definitely possible - but as you can see, parts get more complex :)

Reply
Default user avatar
Default user avatar Mert Simsek | posted 4 years ago

Hi,

I want to get real-time data. Can I do this with redis pub / sub? I own the node.js publisher. I have a symfony subcriber. Can I get the data from node.js and change the twig interface? But i would render twig. How will i send data to twig files?

Reply

Yo Mert Simsek!

Hmm, I am far from an expert on this. However, I believe that you need to create some sort of connection between Redis and HTTP so that you can create a socket connection in JavaScript. For example: http://haishibai.blogspot.c... or https://www.compose.com/art.... The solution likely won't have anything to do with Symfony or Twig: you'll just render a page in Symfony/Twig, then create some JavaScript that will create be the subscriber. You don't need a subscriber in Symfony: if you want your frontend to be able to update based on published messages, then your frontend (JS) needs to be the subscriber.

Cheers!

Reply
Default user avatar
Default user avatar Wiktor Liszkiewicz | posted 4 years ago

Great as always. I really enjoy your tutorials, they make symfony look so easy.
Ps. When will you releas the "Design Pattern" course ?

Reply

Hey Wiktor,

Thanks! That's what we're trying to do ;)

"Design Pattern" course was a bit postpone because its author was changed. Unfortunately, the previous one have no time to continue working on it. Probably, we'll start releasing it in a few tutorials, see our rough roadmap: https://knpuniversity.com/c... . Thanks for your patience.

Cheers!

Reply
Default user avatar
Default user avatar Infete Shine | posted 4 years ago

Hi

Anyone using IvoryCKEditorBundle?

I just want to know how can I add mathjax plugin to it?

Thanks

Reply

I haven't used it personally - so sorry, I can't help!

Reply
Default user avatar
Default user avatar heitoralthmann | posted 4 years ago

Where can I download the course files?

Reply

Hey heitoralthmann

You can find the download button inside any chapter of a tutorial. So, just click on any chapter link, and then, at the right upper corner you will see that button :)

If you have any other troubles, let us know!

1 Reply
Default user avatar
Default user avatar Yan Yong | posted 4 years ago

Thanks Ryan. Your tutorial makes learning symphony quit enjoyable. My suggestion: if you could include a cheatsheet pdf for this course with simple topic that will be very nice.

Reply

Thanks Yan Yong!

That's a cool idea! We'll be updating the courses next year for Symfony 4 - I'll consider doing something like that then - it would be neat!

Cheers!

Reply
Default user avatar
Default user avatar Robert Went | posted 4 years ago

Where do I find the 'coding challenges - practice with real code in an IDE' for each course?

Reply

Hey Robert,

Unfortunately, not every course has coding challenges - this course doesn't have them yet. Coding challenges are displayed in course's table of contents, for example, check this one which has coding challenges: https://knpuniversity.com/s... .

But every course has start and finish code, which you can download and open in your IDE if your don't want to code along with the author during the course. On each course chapter page you can find "Download" / "Course code" buttons, however this option available for those users who has paid subscription or own the course.

Cheers!

Reply
Default user avatar
Default user avatar Robert Went | victor | posted 4 years ago

Ah ok, thanks for the clarification!
I joined yesterday so have access to the code.
Really enjoying it so far.

Reply

Great! We're going to add more coding challenges in the future, actually, we want to have coding challenges for all our course, but it need some time to be done.

Cheers!

Reply
Cat in space

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