Configuration

Introduction

All of the configuration options for the Lumen framework are stored in the .env file.

Accessing Configuration Values

You may easily access your configuration values using the global config helper function from anywhere in your application. The configuration values may be accessed using "dot" syntax, which includes the name of the file and option you wish to access. A default value may also be specified and will be returned if the configuration option does not exist:

  1. $value = config('app.locale');

To set configuration values at runtime, pass an array to the config helper:

  1. config(['app.locale' => 'en']);

Before a configuration file can be used, you should load it into the application using the configure method. This may be done within your bootstrap/app.php file:

  1. $app->configure('app');

Environment Configuration

It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different cache driver locally than you do on your production server. It's easy using environment based configuration.

To make this a cinch, Lumen utilizes the DotEnv PHP library by Vance Lucas. In a fresh Lumen installation, the root directory of your application will contain a .env.example file. You should rename the .env.example file to .env when creating your application.

All of the variables listed in this file will be loaded into the $_ENV PHP super-global when your application receives a request. The env function may be used to retrieve the values of your environment variables:

  1. $debug = env('APP_DEBUG', true);

The second value passed to the env function is the "default value". This value will be used if no environment variable exists for the given key.

Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration.

If you are developing with a team, you may wish to continue including a .env.example file with your application. By putting place-holder values in the example configuration file, other developers on your team can clearly see which environment variables are needed to run your application.

Determining The Current Environment

The current application environment is determined via the APP_ENV variable from your .env file. You may access this value via the environment method on the application instance:

  1. $environment = app()->environment();

You may also pass arguments to the environment method to check if the environment matches a given value. If necessary, you may even pass multiple values to the environment method. If the environment matches any of the given values, the method will return true:

  1. if (app()->environment('local')) {
  2. // The environment is local
  3. }
  4. if (app()->environment('local', 'staging')) {
  5. // The environment is either local OR staging...
  6. }