Drupal's service container can now be compiled and stored as a PHP class file using Symfony's PhpDumper. The resulting class is stored via PhpStorage and benefits from OPcache, which can reduce container build time on warm requests. New Drupal installations default to this mode. Existing sites that upgrade continue to use the legacy PHP array-based container for backward compatibility.
What to do
To switch to the compiled PHP container, add the following line to your settings.php:
$settings['container_base_class'] = \Drupal\Core\DependencyInjection\PhpContainer::class;
New Drupal installations include this line in default.settings.php by default. Sites upgrading from an earlier version will not have it and continue to use the PHP array-based container unless they add it manually.
The container_base_class setting controls which container implementation is used at runtime:
-
\Drupal\Core\DependencyInjection\PhpContainer— uses Symfony'sPhpDumperto compile the container into a PHP class file stored inPhpStorage(typicallysites/default/files/php/service_container/). The file is included on each request and benefits from OPcache. -
\Drupal\Core\DependencyInjection\Container(or unset) — legacy mode using the PHP array-based container serialised to thecache.containercache bin. This remains the default for sites that do not setcontainer_base_classin order to preserve backward compatibility.
Notes
-
The compiled PHP container file is stored in
sites/default/files/php/service_container/(or wherever yourphpPhpStoragedirectory is configured). -
When clearing caches (
drush cror the UI cache rebuild), the old container file is deleted if the container changes..