Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process %kernel.project_dir% from a .env variable?

I added this to my .env:

PHOTOS_DIR=%kernel.project_dir%/var/photos

Of course, when I try to retrieve the value of $_ENV['PHOTOS_DIR'], I get the raw string %kernel.project_dir%/var/photos.

But how can I get the value processed by the Symfony config processor, e.g. /my/project/var/photos?

EDIT: I'm aware that it is simply possible to add this in services.yaml:

parameters:
    photos_dir: '%kernel.project_dir%/var/photos'

But I would like to keep important config data in the .env file.

like image 733
yolenoyer Avatar asked Jul 08 '20 19:07

yolenoyer


People also ask

Can I use variables in .env file?

You can set default values for environment variables using a .env file, which Compose automatically looks for in project directory (parent folder of your Compose file). Values set in the shell environment override those set in the .env file.

How do I store a variable in an .env file?

You can create a . env file at the root of the project directory and put the credential in the file. You can also choose to store your environment variables within a virtual environment; the environment variables will be accessible once the virtual environment is activated.

How do .env files work?

env. example file documents the application's necessary variables and can be committed to version control. This serves as a helpful reference and speeds up the onboarding process for new team members by reducing the amount of time spent digging through the coding to figure out what needs to be set up.


1 Answers

In order to expand %kernel.project_dir%, use %env(resolve:...) in parameters, e.g.:

parameters:
    photos_dir: '%env(resolve:PHOTOS_DIR)%'
like image 149
yolenoyer Avatar answered Sep 19 '22 16:09

yolenoyer