Symfony: 5 – Environments

Environments

Every Symfony application runs within an environment. An environment is a specific set of configuration and loaded bundles, represented by a string. The same application can be run with different configurations by running the application in different environments. Symfony2 comes with three environments defined — dev, test and prod — but you can create your own as well.

Environments are useful by allowing a single application to have a dev environment built for debugging and a production environment optimized for speed. You might also load specific bundles based on the selected environment. For example, Symfony2 comes with the WebProfilerBundle, enabled only in the dev and test environments.

You can view your application in different environments simply by changing the front controller in your browser. To see the application in the dev environment, access the application via the development front controller http://localhost/app_dev.php/hello/Name. If you’d like to see how your application will behave in the production environment, call the prod front controller instead http://localhost/app.php/hello/Name. The test environment is used when running automated tests and cannot be accessed directly through the browser.

Since the prod environment is optimized for speed; the configuration, routing and Twig templates are compiled into flat PHP classes and cached. When viewing changes in the prod environment, you’ll need to clear these cached files and allow them to rebuild:

$ php app/console cache:clear --env=prod --no-debug
# In case of a fatal memory error add --no-warmup (can use a lot of memory)
php app/console cache:clear --env=prod --no-debug --no-warmup
# In case of a fatal memory error you could also try to increase memory
$ php -d memory_limit=256M app/console cache:clear --env=prod --no-debug

The AppKernel class is responsible for actually loading the configuration file of your choice:

// app/AppKernel.php
public function registerContainerConfiguration(LoaderInterface $loader)
{
  $loader->load(
    __DIR__.'/config/config_'.$this->getEnvironment().'.yml'
  );
}

The .yml extension can be changed to .xml or .php if you prefer to use either XML or PHP to write your configuration. Each environment loads its own configuration file. Consider the configuration file for the dev environment:

# app/config/config_dev.yml
imports:
  - { resource: config.yml }
 
framework:
  router: { resource: "%kernel.root_dir%/config/routing_dev.yml" }
  profiler: { only_exceptions: false }
# ...

You can dump the default configuration for a bundle in YAML to the console using the config:dumpreference command. Here is an example of dumping the default FrameworkBundle configuration:

app/console config:dump-reference FrameworkBundle

Symfony2 comes with two web-accessible front controllers: app_dev.php provides the dev environment, and app.php provides the prod environment. All web accesses to Symfony2 normally go through one of these front controllers. (The test environment is normally only used when running unit tests, and so doesn’t have a dedicated front controller. The console tool also provides a front controller that can be used with any environment.)

To make your application respond faster, Symfony2 maintains a cache under the app/cache/ directory. When debug mode is enabled (such as app_dev.php does by default), this cache is flushed automatically whenever you make changes to any code or configuration. When running in debug mode, Symfony2 runs slower, but your changes are reflected without having to manually clear the cache.


Leave a Reply