Symfony: creating a new environment

A typical Symfony application begins with three environments: dev, prod, and test. Creating a new environment is quite easy.

Start by creating a new configuration file:

# app/config/config_myenvironment.yml
imports:
    - { resource: config_prod.yml }
 
framework:
    profiler: { only_exceptions: false }

With this simple addition the application now supports a new environment called myenvironment. This new configuration file imports the configuration from the prod environment and modifies it. This guarantees that the new environment is identical to the prod environment, except for any changes explicitly made here.

Because you’ll want this environment to be accessible via a browser, you should also create a front controller for it. Copy the web/app.php file to web/app_myenvironment.php and edit the environment to be myenvironment:

// web/app_benchmark.php
// ...
 
// change just this line
$kernel = new AppKernel('myenvironment', false);
 
// ...

Debug Mode
The false argument as the second argument to the AppKernel constructor specifies if the application should run in debug mode. This affects many things in the application, such as if errors should be displayed or if cache files are dynamically rebuilt on each request. As of Symfony 2.3, showing errors or not no longer depends on the debug mode. You’ll need to enable that in your front controller by calling enable(). The Debug component provides tools to ease debugging PHP code. They are the foundation of the Symfony full-stack framework, but they can also be used standalone even if you don’t use the framework as they don’t have any mandatory dependencies.

Web Debug Toolbar
To enable the Web Debug Toolbar on myenvironment

// Add this to routing_myenvironment.yml
_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt
 
_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler
 
// Add this to config_myenvironment.yml
framework:
    router:
        resource: "%kernel.root_dir%/config/routing_myenvironment.yml"
        strict_requirements: true
 
web_profiler:
    toolbar: true
 
// Add your environment to AppKernel.php:
if (in_array($this->getEnvironment(), array('dev', 'test', 'myenvironment'))) {
 // ...
}
 
// Clear cache
php app/console cache:clear --env=myenvironment --no-warmup