An environment ServiceProvider for Silex

Installs: 290

Dependents: 1

Stars: 2

Forks: 1

Open Issues: 1

Language: PHP

1.0.1 2014-01-10 10:01 UTC


The development version is for Silex 2 only. Please use the 1.0.x versions for Silex 1

An environment ServiceProvider for Silex which determines the environment (e.g. development, testing or production) an application build around Silex is running in.

Latest Stable Version Latest Unstable Version Build Status Coverage Status Dependency Status


To install the EnvironmentServiceProvider add the following requirement to your composer.json file.

    "required": {
        "simblo/environment-service-provider": "2.0.*@dev"


Register EnvironmentServiceProvider

EnvironmentServiceProvider can be registered through the register() method of \Silex\Application as any other service provider. Just add this line of code after creating the application instance:

$app->register(new EnvironmentServiceProvider());

Configure EnvironmentServiceProvider

While creating a new instance of the provider you can pass an optional array with configuration data like this...

$app->register(new EnvironmentServiceProvider(array(
    'default' => 'bar',
    'filepath' => '/your/config/dir',
    'filename' => '',
    'environments' => array('foo', 'bar'),

The example above also shows all possible options that can be used to configure EnvironmentServiceProvider to your needs. Here are the options explained in detail.


Defaults to development. The default option can be used to set a fallback environment if the detection fails or you did not set an environment on purpose. default must be a valid environment which is present in environments

If default is set to false EnvironmentServiceProvider will throw an exception instead of falling back to a default environment if no environment is detected.


Defaults to false. As long as filepath is set to false the provider will not check for an environment file which contains the environment the application should run in. If this is set to an appropriate path containing the environment file it's content is used as environment as long as it is a valid environment present in environments. No trailing slash and do not include the filename of the environment file since he is set in the next option.


Defaults to .setenv. This option represents the name of the file containing the environment the application should run in. Change this if you do not like the default setting or can't use it for any reason.


Defaults to SILEX_ENVIRONMENT. If no filepath is set or the environment file was not readable the provider uses an environment variable to determine the application environment. If for any reason you can't use the default value or feel that it does not fit your application feel free to change this option.


Defaults to development, testing, staging and production. The application can only run in environments known by the provider. This option is an array with strings representing known or valid enviromnents the application is allowed to run in. The default value should fit any needs but maybe your application is a special one ;)

Setting an environment

To set an application environment the provider is able to determine there are two different possible methods.

This can either be achieved by setting an environment variable named SILEX_ENVIRONMENT like shown in the following examples


or it can be set in an environment file. In this case a valid filepath must be set in the options. Just create the file .setenv in a location which can be accesses by PHP and write down the desired environment inside.

Retrieving the environment

Now you are able to access the environment by using the environment property of your Silex Application like this:

$env = $app['environment']