Handling environment-specific configuration in Zend Expressive apps


#1

I’m looking for some good practices for handling environment-based configuration variables (database endpoints, credentials for external services, etc) in Zend Expressive apps.

"12 factor” methodology encourages to keep them in server’s environment variables. Providers like AWS have built-in support for propagating them, simplifying application deployment and management.

Symfony deals with this problem with DotEnv component. In development mode, all variables can be defined in “.env” file. In ZF ecosystem, components tend to keep this data in ConfigProviders. While this is a bit easier to understand at the beginning (the app has only one “config”), it makes it harder work on a project with a bigger team and more complex deployment strategies.

So, what’s your way of handling credentials between different environments? Is anyone here using environment variables to store this configuration? How exactly?


#2

I do use environment variables, and I have a GetEnv() class (functional interface via __invoke()) that asserts that the environment variable is successfully retrieved via \getenv().

You can simply use it in any of the configuration files, since the configuration files are just PHP files.

.env loading is something I leave to docker-compose.


#3

I’m doing similar to @ocramius — using getenv() or a wrapper directly in my *.global.php files, and then defining the env variables in my docker configuration or AWS configuration.


#4

Ditto to what @ocramius and @matthew said. We have plain PHP configuration files that use getenv() to grab sensitive values. Provisioning ENV variables is left to the devops team.