Chapters
25 Chapters
|
2:34:05
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"knplabs/knp-time-bundle": "^1.18", // v1.19.0
"symfony/asset": "6.1.*", // v6.1.0-RC1
"symfony/console": "6.1.*", // v6.1.0-RC1
"symfony/dotenv": "6.1.*", // v6.1.0-RC1
"symfony/flex": "^2", // v2.4.5
"symfony/framework-bundle": "6.1.*", // v6.1.0-RC1
"symfony/http-client": "6.1.*", // v6.1.0-RC1
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/runtime": "6.4.3", // v6.4.3
"symfony/twig-bundle": "6.1.*", // v6.1.0-RC1
"symfony/ux-turbo": "^2.0", // v2.1.1
"symfony/webpack-encore-bundle": "^1.13", // v1.14.1
"symfony/yaml": "6.1.*", // v6.1.0-RC1
"twig/extra-bundle": "^2.12|^3.0", // v3.4.0
"twig/twig": "^2.12|^3.0" // v3.4.0
},
"require-dev": {
"symfony/debug-bundle": "6.1.*", // v6.1.0-RC1
"symfony/maker-bundle": "^1.41", // v1.42.0
"symfony/stopwatch": "6.1.*", // v6.1.0-RC1
"symfony/web-profiler-bundle": "6.1.*" // v6.1.0-RC1
}
}
8 Comments
Previously, we saw that yaml configurations in a file are parsed in the order the declared.
Regarding parameters, this seems like an exception to this.
Regarding cache, when framework.cache.app is parsed, you would expect it to contain the previously declared cache_adapter parameter, which is not environment aware, and then when it is overridden in when@dev, that this would have no effect, because cache.app is already declared.
Isn't this a bit confusing?
Hey Alkiviadis,
Not sure I completely understand you here, but you're probably overthinking about it. First of all, in the dev env when we're changing a config file - the cache will be automatically recompiled so any new changes you made in the config will be re-parsed on the next request. In prod Symfony mode it's a bit different, when you change a config file - you will need to clear the cache manually to let the system notice your changes. So, in both cases the cache will be re-compiled and so the new parameter values will be used.
I hope this helps.
Cheers!
Thank you for your answer, but I am actually talking about the order the configurations are read, not when and if the cache is cleared.
Specifically while on (
cache.yaml) we had in the middle of course:even when we are at dev environment, framework.cache.app already got assigned a value from the first cache_adapter declaration, before the cache_adapter parameter got reassigned with new value later. But the framework.cache.app got the final value of cache_adapter, which seemed strange to me at first.
So, are all parameters parsed/set first, and then anything else?
Hey Alkiviadis,
It's important do understand that Symfony first gathers all the configuration from separate config files and only then trying to resolve parameter values. Actually, the parameter values will be resolved even later only when you fetch the service from the service container - the container will instantiate the service object and resolve all the parameter values it has.
Yeah, it probably might be confusing at first sight, but that's just an overwritten operation. Yes, when the config file is parsed - the
framework.cache.appwill be set to the%cache_adapter%(but it's just instructions, it's still not resolved actual value yet). And later if you're in adevmode, thewhen@devwill be parsed as well which will overwrite the values that were already set before (if any).If it's confusing you - you can avoid
when@devsyntax and put the dev config inside theconfig/packages/devfolder, but behind the scene it will work the same anyway: it first will gather all the default config (for all environments), and only then overwrite any whiles with env-specific config.So, even though it's an overwriting operation in some cases - that's actually really powerful and flexible: you have the default config values but can overwrite some values with other ones for specific enviroments.
I hope this clarifies things!
Cheers!
Coming from Laravel, I'm a bit confused where I can put general config for my app. In Laravel I could just create a
MyApp.phpfile in theconfigfolder and then read it from wherever. So say I have some project-wide config in my Symfony app, where should I put it? I tried creating aapp.yamlfile inconfig/packagesbut when I rundebug:config appit says "No extensions with configuration available for "app"".Hey Jim,
Yeah, that
config/packages/folder is special and contains bundle-specific configs :) Try to put your config in justconfig/dir, it should work well this way :)Cheers!
Thanks for the reply Victor. Unfortunately I still get the same error, also after clearing the cache. So in the
configdirectory I have put a fileapp.yaml, which looks a bit like this:Then when I run
debug:config appI get the error described above. And when, in a console command, I haveI get the error "You have requested a non-existent parameter "app.foo"".
Hey Jim-B,
Wait, if you drop that
app.yamlfile from theconfig/packages/and put intoconfig/- you should not have thatNo extensions with configuration available for "app"error anymore, as nobody will parse your file at all. And this means you would need to parse that file yourself.But in your second example I see you just want to add some data to the config so that system knows about your data. If so, you need to add "parameters" - that's how it's called in Symfony. For this, you can open
config/services.yamland underparameters:key add your data. Then you will be able to fetch those parameters with the code you showed in the end :)Cheers!
"Houston: no signs of life"
Start the conversation!