Use environment variables and a .env file in Neos Flow Framework

Environment variables for third party tokens and similar, can be accessed for configuration in Neos Flow.

Example: Use in Settings.yaml for database connection

If you have environment variable set, you can use them by setting a value similar to %env:ENVIRONMENT_VARIABLE_NAME% with ENVIRONMENT_VARIABLE_NAME being the name of the key.

For connection to the database it could look like this

Neos:
  Flow:
    persistence:
      backendOptions:
        dbname: '%env:DATABASE_NAME%'
        port: '%env:DATABASE_PORT%'
        user: '%env:DATABASE_USER%'
        password: '%env:DATABASE_PASSWORD%'
        host: '%env:DATABASE_HOST%'

Example: Use in configuration of objects with Objects.yaml

With Objects.yaml in Neos Flow, you can set values of contsructor arguments and properties. In this file, you can also pass environment variables by using the value keyword.

In this example I set two constructor argument, with values that is environment independent, and can be changed on the environment, without changing code or duplicating the implementation

Vendor\Application\Consumer\Provider:
  arguments:
    1:
      value: '%env:CONSUMER_API_CLIENT_ID%'
    2:
      value: '%env:CONSUMER_API_CLIENT_SECRET%'

Use .env file in your local development environment

The PHP package helhum/dotenv-connector makes it simply for you to use a .env file with similar values of, what you are setting in your hosting environment.

As an example I have a .env file like this

DATABASE_DRIVER="pdo_mysql"
DATABASE_CHARSET="utf8mb4"
DATABASE_HOST="127.0.0.1"
DATABASE_PORT="3306"
DATABASE_NAME="application"
DATABASE_USER="application"
DATABASE_PASSWORD="password"

And simply because I’ve installed helhum/dotenv-connector I can now use the Settings.yaml file shown above, to configure the database connection.

At the same time, my staging and production environments has the same variables and therefore, the configuration file is a part of the repository and no code changes required