How To Create Symfony Applications with Multiple Kernels

How To Create Symfony Applications with Multiple Kernels

Caution

Creating applications with multiple kernels is no longer recommended by Symfony. Consider creating multiple small applications instead.

In most Symfony applications, incoming requests are processed by the public/index.php front controller, which instantiates the src/Kernel.php class to create the application kernel that loads the bundles and handles the request to generate the response.

This single kernel approach is a convenient default, but Symfony applications can define any number of kernels. Whereas environments run the same application with different configurations, kernels can run different parts of the same application.

These are some of the common use cases for creating multiple kernels:

  • An application that defines an API could define two kernels for performance reasons. The first kernel would serve the regular application and the second one would only respond to the API requests, loading less bundles and enabling less features;
  • A highly sensitive application could define two kernels. The first one would only load the routes that match the parts of the application exposed publicly. The second kernel would load the rest of the application and its access would be protected by the web server;
  • A micro-services oriented application could define several kernels to enable/disable services selectively turning a traditional monolith application into several micro-applications.

Adding a new Kernel to the Application

Creating a new kernel in a Symfony application is a three-step process:

  1. Create a new front controller to load the new kernel;
  2. Create the new kernel class;
  3. Define the configuration loaded by the new kernel.

The following example shows how to create a new kernel for the API of a given Symfony application.

Step 1) Create a new Front Controller

Instead of creating the new front controller from scratch, it’s easier to duplicate the existing one. For example, create public/api.php from public/index.php.

Then, update the code of the new front controller to instantiate the new kernel class instead of the usual Kernel class:

  1. // public/api.php
  2. // ...
  3. $kernel = new ApiKernel(
  4. $_SERVER['APP_ENV'] ?? 'dev',
  5. $_SERVER['APP_DEBUG'] ?? ('prod' !== ($_SERVER['APP_ENV'] ?? 'dev'))
  6. );
  7. // ...

Tip

Another approach is to keep the existing index.php front controller, but add an if statement to load the different kernel based on the URL (e.g. if the URL starts with /api, use the ApiKernel).

Step 2) Create the new Kernel Class

Now you need to define the ApiKernel class used by the new front controller. The easiest way to do this is by duplicating the existing src/Kernel.php file and make the needed changes.

In this example, the ApiKernel will load less bundles than the default Kernel. Be sure to also change the location of the cache, logs and configuration files so they don’t collide with the files from src/Kernel.php:

  1. // src/ApiKernel.php
  2. use Symfony\Component\Config\Loader\LoaderInterface;
  3. use Symfony\Component\DependencyInjection\ContainerBuilder;
  4. use Symfony\Component\HttpKernel\Kernel;
  5. class ApiKernel extends Kernel
  6. {
  7. use MicroKernelTrait;
  8. public function registerBundles()
  9. {
  10. // load only the bundles strictly needed for the API
  11. $contents = require $this->getProjectDir().'/config/api_bundles.php';
  12. foreach ($contents as $class => $envs) {
  13. if ($envs[$this->environment] ?? $envs['all'] ?? false) {
  14. yield new $class();
  15. }
  16. }
  17. }
  18. public function getProjectDir(): string
  19. {
  20. return \dirname(__DIR__);
  21. }
  22. public function getCacheDir(): string
  23. {
  24. return $this->getProjectDir().'/var/cache/api/'.$this->getEnvironment();
  25. }
  26. public function getLogDir(): string
  27. {
  28. return $this->getProjectDir().'/var/log/api';
  29. }
  30. public function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
  31. {
  32. $container->addResource(new FileResource($this->getProjectDir().'/config/api_bundles.php'));
  33. $container->setParameter('container.dumper.inline_factories', true);
  34. $confDir = $this->getProjectDir().'/config/api';
  35. $loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
  36. $loader->load($confDir.'/{packages}/'.$this->environment.'/*'.self::CONFIG_EXTS, 'glob');
  37. $loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
  38. $loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob');
  39. }
  40. protected function configureRoutes(RouteCollectionBuilder $routes): void
  41. {
  42. $confDir = $this->getProjectDir().'/config/api';
  43. // ... load only the config routes strictly needed for the API
  44. }
  45. }

Step 3) Define the Kernel Configuration

Finally, define the configuration files that the new ApiKernel will load. According to the above code, this config will live in one or multiple files stored in config/api/ and config/api/ENVIRONMENT_NAME/ directories.

The new configuration files can be created from scratch when you load only a few bundles, because it will be small. Otherwise, duplicate the existing config files in config/packages/ or better, import them and override the needed options.

Executing Commands with a Different Kernel

The bin/console script used to run Symfony commands always uses the default Kernel class to build the application and load the commands. If you need to run console commands using the new kernel, duplicate the bin/console script and rename it (e.g. bin/api).

Then, replace the Kernel instance by your own kernel instance (e.g. ApiKernel). Now you can run commands using the new kernel (e.g. php bin/api cache:clear).

Note

The commands available for each console script (e.g. bin/console and bin/api) can differ because they depend on the bundles enabled for each kernel, which could be different.

Rendering Templates Defined in a Different Kernel

If you follow the Symfony Best Practices, the templates of the default kernel will be stored in templates/. Trying to render those templates in a different kernel will result in a There are no registered paths for namespace “__main__” error.

In order to solve this issue, add the following configuration to your kernel:

  1. # config/api/twig.yaml
  2. twig:
  3. paths:
  4. # allows to use api/templates/ dir in the ApiKernel
  5. "%kernel.project_dir%/api/templates": ~

Running Tests Using a Different Kernel

In Symfony applications, functional tests extend by default from the Symfony\Bundle\FrameworkBundle\Test\WebTestCase class. Inside that class, a method called getKernelClass() tries to find the class of the kernel to use to run the application during tests. The logic of this method does not support multiple kernel applications, so your tests won’t use the right kernel.

The solution is to create a custom base class for functional tests extending from WebTestCase class and overriding the getKernelClass() method to return the fully qualified class name of the kernel to use:

  1. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  2. // tests needing the ApiKernel to work, now must extend this
  3. // ApiTestCase class instead of the default WebTestCase class
  4. class ApiTestCase extends WebTestCase
  5. {
  6. protected static function getKernelClass()
  7. {
  8. return 'App\ApiKernel';
  9. }
  10. // this is needed because the KernelTestCase class keeps a reference to
  11. // the previously created kernel in its static $kernel property. Thus,
  12. // if your functional tests do not run in isolated processes, a later run
  13. // test for a different kernel will reuse the previously created instance,
  14. // which points to a different kernel
  15. protected function tearDown()
  16. {
  17. parent::tearDown();
  18. static::$class = null;
  19. }
  20. }

Adding more Kernels to the Application

If your application is very complex and you create several kernels, it’s better to store them in their own directories instead of messing with lots of files in the default src/ directory:

  1. project/
  2. ├─ src/
  3. ├─ ...
  4. └─ Kernel.php
  5. ├─ api/
  6. ├─ ...
  7. └─ ApiKernel.php
  8. ├─ ...
  9. └─ public/
  10. ├─ ...
  11. ├─ api.php
  12. └─ index.php

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.