Environment Configuration

Most applications use a set of different environments to validate new features and test the fixes for bugs before letting the end users see it. li3’s Environment class offers a way to organize settings and make logic in your application conditional based on where the application thinks it resides.

Rather than wrapping conditionals around all of your database settings and API keys and endpoints, you can use the Environment class to direct the application automatically, based on context.

li3 offers a default set of environments (development, test, and production) as well as a starting point for determining which one the application resides in. This guide covers the process of showing li3 how to detect and define your different environments and use them in configuration details.

Getting Started: Detection

The first step in using different environmental settings is telling li3 how to detect where it is operating from. There are a number of clues often used in determining the environment: IP address or ranges, hostname pattern matching, or possibly even looking at the filesystem or database.

Environment detection is determined by default as follows:

  • The request is local (IP address 127.0.0.1, etc.): development.
  • The request URL or hostname starts with “test”: test.
  • Neither of the above: production.

To customize this behavior, use the Environment::is() method inside a bootstrap file. Supply a closure that inspects an incoming Request and returns the name of the environment deteted. Here’s a simple example:

  1. Environment::is(function($request){
  2. $host = $request->env('HTTP_HOST');
  3. if ($host == 'project.local' || $host == 'localhost') {
  4. return 'development';
  5. }
  6. if (preg_match('/^qa/', $host)) {
  7. return 'qa';
  8. }
  9. if (preg_match('/beta/', $host)) {
  10. return 'staging';
  11. }
  12. return 'production';
  13. });

This code defines four custom environments: development if the hostname matches a predetermined set of hostnames, qa if the hostname begins with “qa” (qa1.example.com, qa2.example.com), and staging if the hostname contains “beta” (beta.example.com, www.example-beta.com).

If none of those conditions are met, the default is production.

Environment-Specific Settings

Once detection is configured, you’re able to set environment-specific variables inside of your bootstrap files.

  1. Environment::set('development', ['service_endpoint', 'dev.service.example.com']);
  2. Environment::set('staging' , ['service_endpoint', 'beta.service.example.com']);
  3. Environment::set('qa' , ['service_endpoint', 'qa1.service.example.com']);
  4. Environment::set('production' , ['service_endpoint', 'www.service.example.com']);
  5. // If run on my local system:
  6. Environment::get('service_endpoint'); // 'dev.service.example.com'

Adaptable Environment Settings

Subclasses of the core Adaptable class (Logger, Connections, Cache, Session, etc.) can also be configured on an environmental basis. For core functionality, this is much cleaner than large sets of Environment::set calls in your bootstrap files.

The most common example is configuring different database connections based on environment. Here’s a quick example:

  1. Connections::add('default', [
  2. 'production' => [
  3. 'type' => 'database',
  4. 'adapter' => 'MySql',
  5. 'host' => 'live.example.com',
  6. 'login' => 'mysqluser',
  7. 'password' => 's3cr3tg035h3rE',
  8. 'database' => 'app-production'
  9. ],
  10. 'development' => [
  11. 'type' => 'database',
  12. 'adapter' => 'MySql',
  13. 'host' => 'localhost',
  14. 'login' => 'root',
  15. 'password' => '4d1fFeR3n75eCr37',
  16. 'database' => 'app'
  17. ]
  18. ]);

While it is handy for using the same types of technologies in each environment, this mechanism allows you to switch between engines as well. For example, using different kinds of Cache engines in each environment:

  1. Cache::config([
  2. 'userData' => [
  3. 'development' => ['adapter' => 'File'],
  4. 'production' => ['adapter' => 'Memcache']
  5. ]
  6. ]);

For more information about creating your own classes that utilize Adaptable and Environment for automatically changing settings, see the Adaptable guide in the Advanced Tasks section.