3

By default Symfony requires configuration to set on web server rather than within the application. In my case, setting these variables on the Server is very complicated. So, what I want is to set these variables inside the application (php or yaml) without DotEnv and web server env variables.

These are the environment variables that I need to define:

APP_ENV 
APP_SECRET
DATABASE_URL
MONGODB_URL
MAILER_URL

Here is what I found except the APP_ENV that is not working in any place:

APP_ENV: ???
APP_SECRET: config/packages/framework.yaml
DATABASE_URL: config/packages/doctrine.yaml
MONGODB_URL: config/packages/doctrine_mongodb.yaml
MAILER_URL: config/packages/swiftmailer.yaml

Where do we define APP_ENV in Symfony 4?

1
  • 1
    I don't think you have much choice but to hardcode it into config/bootstrap.php. Commented Apr 30, 2019 at 17:42

1 Answer 1

4

You can use .env.local.php to return an array.

This file is usually generated by running composer dump-env [env], but nothing stops you from creating it manually.

This file is loaded before DotEnv loads ".env" files, and it's a simple array that's merged with $_SERVER and $_ENV. If this file exists, the DotEnv component is not used at all.

E.g. you could return something like:

return [
  'APP_ENV'           => 'prod',
  'APP_SECRET'        => 'verysecretmuchobscure',
  'CORS_ALLOW_ORIGIN' => '^https?://localhost(:[0-9]+)?$',
  'DATABASE_URL'      => 'mysql://db_user:[email protected]:3306/db_name',
  'JWT_SECRET_KEY'    => '%kernel.project_dir%/var/keys/private.pem',
  'JWT_PUBLIC_KEY'    => '%kernel.project_dir%/var/keys/public.pem',
  'JWT_PASSPHRASE'    => 'anotherthing',
];

All this applies if you are using Symfony 4.1+. In your question you say Symfony 4, but do not specify which minor version you are using.

If you are on 4.0, you'll need to follow the steps detailed here to upgrade your application:

  1. Copy bootstrap.php from the repo
  2. Copy index.php from the repo, or update your index.php so it matches that one in regards to requiring bootstrap.php(in case your index.php had modifications on it)
  3. The same for your console script, if exists.
  4. Update your main .gitignore
  5. Rename/move/delete your existing .env files.
Sign up to request clarification or add additional context in comments.

5 Comments

Interesting. I had no idea that bootstrap.php had been added back in. Here are some more details if anyone is interested.
@yivi That's the answer I'm looking for. However, it lacks details on how to load and use .env.local.php file because I used Symfony 4.0 which doesn't have bootstrap.php.
@Cerad you provided a reference which is more complete, I actually figured it out from your comment here rather the answer from yivi. It provides the instruction on how to update from Symfony before Nov 2018, while the answer from yivi only apply to the Symfony after Nov 2018. I would have accepted your answer if you posted it as an answer.
Sorry @Darith, you didn't specify 4 .0 in your Q, and I assumed you meant the latest Symfony 4 release. This has been part of Symfony since 4. 1. I'll include the link the upgrade information in the answer anyway.
@yivi, I will be more specific next time when asking questions, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.