Chapters
- Course CodeSubscribe to download the code!Subscribe to download the code!
- This VideoSubscribe to download the video!Subscribe to download the video!
- SubtitlesSubscribe to download the subtitles!Subscribe to download the subtitles!
- Course ScriptSubscribe 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!
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.
With a Subscription, click any sentence in the script to jump to that part of the video!
LoginSubscribeEnvironment variables are for values that differ dependingon the environment we're developing in, like locally vs production.The most common example of this is the database connection details.We can setreal environment variables in our operating system,and while many cloud hosting platforms make it super easy to set these variables,it's not the easiest thing to do locally.Symfony also has this.env file which helps make life easy,especially during development.
Okay, here's the plan: We want ouriss_location_cache_ttl valueto be differentlocally versus on production.In ourprod environment, we want our cache to lastlonger than the 5 seconds that we have now.
Show Lines | // ... lines 1 - 5 |
| parameters: | |
| iss_location_cache_ttl:5 | |
Show Lines | // ... lines 8 - 26 |
The easiest way to do this would be to create acustom environment variable and set itto a different value for each environment -dev andprod.
Creating and Reading Environment Variables
In our.env file, down here, writeISS_LOCATION_CACHE_TTL in all uppercase letters,which is standard for environment variables.Let's set this to5 by default.
Show Lines | // ... lines 1 - 20 |
| ISS_LOCATION_CACHE_TTL=5 |
Now, over inservices.yaml, we're going to keep theiss_location_cache_ttl parameter,but instead of5, let's set it to the environment variable we just created.To do that, we need to leverage a special syntax.Write'%env()' and select our newISS_LOCATION_CACHE_TTL environment variable.Nice!
Show Lines | // ... lines 1 - 5 |
| parameters: | |
| iss_location_cache_ttl:'%env(ISS_LOCATION_CACHE_TTL)%' | |
Show Lines | // ... lines 8 - 26 |
To debug this,in/src/Controller/MainController.php, findhomepage().Insidethat, belowResponse, let's writedd($this->getParameter())and addiss_location_cache_ttl.
Show Lines | // ... lines 1 - 17 |
| classMainControllerextendsAbstractController | |
| { | |
Show Lines | // ... line 20 |
| publicfunctionhomepage( | |
Show Lines | // ... lines 22 - 24 |
| ):Response{ | |
| dd($this->getParameter('iss_location_cache_ttl')); | |
Show Lines | // ... lines 27 - 40 |
| } | |
| } |
Environment Variable Processors
Back at the browser, refresh.There's5.It's subtle, but you may have noticed that this value is a string right now.All environment variable values are just simple strings by default, but Symfony has a waytotypecast them to adifferent type.They're called "environment variable processors",and one of them can help us typecast this to an integer instead.
Back in our code, openservices.yaml.Before the environment variable, addint:.
Show Lines | // ... lines 1 - 5 |
| parameters: | |
| iss_location_cache_ttl:'%env(int:ISS_LOCATION_CACHE_TTL)%' | |
Show Lines | // ... lines 8 - 26 |
If we go refresh...now we have a real integer5.If we were deploying this project to production, we'd probably wantto set thisISS_LOCATION_CACHE_TTL variable to something a little longer, like60,so it will cache the data for1 minute instead of 5 seconds.The shorter time frame is just more practical while we're testing things out.
The.env.local File
While we're here, I want to talk about someother.env files.This.env file is committed to your Git repository, and as you see here,when I make changes to it, those changes are unstaged.So if you have somesecrets that you don't want to commit to your Git repository,like sensitive tokens, passwords, etc, you can create adifferent file -.env.local.This one isignored by Git, which we can see in our.gitignore file.Any sensitive info can be stored here and it won't be committed to the repository.We could, for example, move thisAPP_SECRET environment variable into our.env.local file.Inside our.env file, we can keep this empty or set it to some fake value.It's generally good practice to still keep the environment variables in the.envso that other developers can see them and set real values for them intheir.env.local file.This was just an example, so we can change this back.
Debugging Environment Variables
Along with these two files, there's also the less commonly used.env.test and.env.prod.Those are only loaded in thetest andprod environments respectively.We also have a handy command to debug environment variables.At your terminal, run:
bin/console debug:dotenvThis can help us understand what order thosefiles will be loaded in and, as a bonus,it lists all of the environment variables it found in each file.So far, we only have three and we can see their actual valuesand in which files those values are set.
If you'reserious about securing your sensitive information, Symfony has a special toolfor this called the "Secrets Vault".If you Google "Symfony secrets", one of the top results is "Howto Keep Sensitive Information Secret", which leads us to some documentation.With the Secrets Vault, we can safely commit environment variables to our Git repositorybecause they're encrypted andcan't be read withoutdecrypting.If you need this level of data protection, I encourage you to read the docs or checkout our related videos on SymfonyCasts.I'll revert the changes we made to our homepage and remove thisdd().We don't need that anymore.
Next: Let's talk more aboutauto-configuration.
Comments

"Houston: no signs of life"
Start the conversation!
What PHP libraries does this tutorial use?
// composer.json{ "require": { "php": ">=8.2", "ext-ctype": "*", "ext-iconv": "*", "knplabs/knp-time-bundle": "^2.2", // v2.4.0 "php-cs-fixer/shim": "^3.46", // v3.46.0 "phpdocumentor/reflection-docblock": "^5.3", // 5.3.0 "phpstan/phpdoc-parser": "^1.25", // 1.25.0 "symfony/asset": "7.0.*", // v7.0.3 "symfony/asset-mapper": "7.0.*", // v7.0.2 "symfony/console": "7.0.*", // v7.0.2 "symfony/dotenv": "7.0.*", // v7.0.2 "symfony/flex": "^2", // v2.4.3 "symfony/framework-bundle": "7.0.*", // v7.0.2 "symfony/http-client": "7.0.*", // v7.0.2 "symfony/monolog-bundle": "^3.0", // v3.10.0 "symfony/property-access": "7.0.*", // v7.0.0 "symfony/property-info": "7.0.*", // v7.0.0 "symfony/runtime": "7.0.*", // v7.0.0 "symfony/serializer": "7.0.*", // v7.0.2 "symfony/stimulus-bundle": "^2.13", // v2.13.3 "symfony/twig-bundle": "7.0.*", // v7.0.0 "symfony/ux-turbo": "^2.13", // v2.13.2 "symfony/yaml": "7.0.*", // v7.0.0 "symfonycasts/tailwind-bundle": "^0.7.1", // v0.7.1 "twig/extra-bundle": "^2.12|^3.0", // v3.8.0 "twig/twig": "^2.12|^3.0" // v3.8.0 }, "require-dev": { "symfony/debug-bundle": "7.0.*", // v7.0.0 "symfony/maker-bundle": "^1.52", // v1.53.0 "symfony/stopwatch": "7.0.*", // v7.0.0 "symfony/web-profiler-bundle": "7.0.*" // v7.0.2 }}