|
| 1 | +.. index:: |
| 2 | + single: kernel, performance |
| 3 | + |
| 4 | +How To Create Symfony Applications with Multiple Kernels |
| 5 | +======================================================== |
| 6 | + |
| 7 | +In most Symfony applications, incoming requests are processed by the |
| 8 | +``web/app.php`` front controller, which instantiates the ``app/AppKernel.php`` |
| 9 | +class to create the application kernel that loads the bundles and handles the |
| 10 | +request to generate the response. |
| 11 | + |
| 12 | +This single kernel approach is a convenient default provided by the Symfony |
| 13 | +Standard edition, but Symfony applications can define any number of kernels. |
| 14 | +Whereas :doc:`environments </configuration/environments>` execute the same |
| 15 | +application with different configurations, kernels can execute different parts |
| 16 | +of the same application. |
| 17 | + |
| 18 | +These are some of the common use cases for creating multiple kernels: |
| 19 | + |
| 20 | +* An application that defines an API could define two kernels for performance |
| 21 | + reasons. The first kernel would serve the regular application and the second |
| 22 | + one would only respond to the API requests, loading less bundles and enabling |
| 23 | + less features; |
| 24 | +* A highly sensitive application could define two kernels. The first one would |
| 25 | + only load the routes that match the parts of the application exposed publicly. |
| 26 | + The second kernel would load the rest of the application and its access would |
| 27 | + be protected by the web server; |
| 28 | +* A micro-services oriented application could define several kernels to |
| 29 | + enable/disable services selectively turning a traditional monolith application |
| 30 | + into several micro-applications. |
| 31 | + |
| 32 | +Adding a new Kernel to the Application |
| 33 | +-------------------------------------- |
| 34 | + |
| 35 | +Creating a new kernel in a Symfony application is a three-step process: |
| 36 | + |
| 37 | +1. Create a new front controller to load the new kernel; |
| 38 | +2. Create the new kernel class; |
| 39 | +3. Define the configuration loaded by the new kernel. |
| 40 | + |
| 41 | +The following example shows how to create a new kernel for the API of a given |
| 42 | +Symfony application. |
| 43 | + |
| 44 | +Step 1) Create a new Front Controller |
| 45 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 46 | + |
| 47 | +Instead of creating the new front controller from scratch, it's easier to |
| 48 | +duplicate the existing ones. For example, create ``web/api_dev.php`` from |
| 49 | +``web/app_dev.php`` and ``web/api.php`` from ``web/app.php``. |
| 50 | + |
| 51 | +Then, update the code of the new front controllers to instantiate the new kernel |
| 52 | +class instead of the usual ``AppKernel`` class:: |
| 53 | + |
| 54 | + // web/api.php |
| 55 | + // ... |
| 56 | + $kernel = new ApiKernel('prod', false); |
| 57 | + // ... |
| 58 | + |
| 59 | + // web/api_dev.php |
| 60 | + // ... |
| 61 | + $kernel = new ApiKernel('dev', true); |
| 62 | + // ... |
| 63 | + |
| 64 | +.. tip:: |
| 65 | + |
| 66 | + Another approach is to keep the existing front controller (e.g. ``app.php`` and |
| 67 | + ``app_dev.php``), but add an ``if`` statement to load the different kernel based |
| 68 | + on the URL (e.g. if the URL starts with ``/api``, use the ``ApiKernel``). |
| 69 | + |
| 70 | +Step 2) Create the new Kernel Class |
| 71 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 72 | + |
| 73 | +Now you need to define the ``ApiKernel`` class used by the new front controller. |
| 74 | +The easiest way to do this is by duplicating the existing ``app/AppKernel.php`` |
| 75 | +file and make the needed changes. |
| 76 | + |
| 77 | +In this example, the ``ApiKernel`` will load less bundles than AppKernel. Be |
| 78 | +sure to also change the location of the cache, logs and configuration files so |
| 79 | +they don't collide with the files from ``AppKernel``:: |
| 80 | + |
| 81 | + // app/ApiKernel.php |
| 82 | + use Symfony\Component\HttpKernel\Kernel; |
| 83 | + use Symfony\Component\Config\Loader\LoaderInterface; |
| 84 | + |
| 85 | + class ApiKernel extends Kernel |
| 86 | + { |
| 87 | + public function registerBundles() |
| 88 | + { |
| 89 | + // load only the bundles strictly needed for the API... |
| 90 | + } |
| 91 | + |
| 92 | + public function getCacheDir() |
| 93 | + { |
| 94 | + return dirname(__DIR__).'/var/cache/api/'.$this->getEnvironment(); |
| 95 | + } |
| 96 | + |
| 97 | + public function getLogDir() |
| 98 | + { |
| 99 | + return dirname(__DIR__).'/var/logs/api'; |
| 100 | + } |
| 101 | + |
| 102 | + public function registerContainerConfiguration(LoaderInterface $loader) |
| 103 | + { |
| 104 | + $loader->load($this->getRootDir().'/config/api/config_'.$this->getEnvironment().'.yml'); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +In order for the autoloader to find your new ``ApiKernel``, make sure you add it |
| 109 | +to your ``composer.json`` autoload section: |
| 110 | + |
| 111 | + { |
| 112 | + "...": "..." |
| 113 | + |
| 114 | + "autoload": { |
| 115 | + "psr-4": { "": "src/" }, |
| 116 | + "classmap": [ "app/AppKernel.php", "app/AppCache.php", "app/ApiKernel.php" ] |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +Then, run ``composer install`` to dump your new autoload config. |
| 121 | + |
| 122 | +Step 3) Define the Kernel Configuration |
| 123 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 124 | + |
| 125 | +Finally, define the configuration files that the new ``ApiKernel`` will load. |
| 126 | +According to the above code, this config will live in the ``app/config/api/`` |
| 127 | +directory. |
| 128 | + |
| 129 | +The new configuration can be created from scratch when you load just a few |
| 130 | +bundles, because it will be very simple. Otherwise, duplicate the existing |
| 131 | +config files or better, import them and override the needed options: |
| 132 | + |
| 133 | +.. code-block:: yaml |
| 134 | +
|
| 135 | + # app/config/api/config_dev.yml |
| 136 | + imports: |
| 137 | + - { resource: ../config_dev.yml } |
| 138 | +
|
| 139 | + # override option values ... |
| 140 | +
|
| 141 | +Executing Commands with a Different Kernel |
| 142 | +------------------------------------------ |
| 143 | + |
| 144 | +The ``bin/console`` script used to run Symfony commands always uses the default |
| 145 | +``AppKernel`` class to build the application and load the commands. If you need |
| 146 | +to execute console commands using the new kernel, duplicate the ``bin/console`` |
| 147 | +script and rename it (e.g. ``bin/api``). |
| 148 | + |
| 149 | +Then, replace the ``AppKernel`` instantiation by your own kernel instantiation |
| 150 | +(e.g. ``ApiKernel``) and now you can execute commands using the new kernel |
| 151 | +(e.g. ``php bin/api cache:clear``) Now you can use execute commands using the |
| 152 | +new kernel. |
| 153 | + |
| 154 | +.. note:: |
| 155 | + |
| 156 | + The commands available for each console script (e.g. ``bin/console`` and |
| 157 | + ``bin/api``) can differ because they depend on the bundles enabled for each |
| 158 | + kernel, which could be different. |
| 159 | + |
| 160 | +Rendering Templates Defined in a Different Kernel |
| 161 | +------------------------------------------------- |
| 162 | + |
| 163 | +If you follow the Symfony Best Practices, the templates of the default kernel |
| 164 | +will be stored in ``app/Resources/views/``. Trying to render those templates in |
| 165 | +a different kernel will result in a *There are no registered paths for |
| 166 | +namespace "__main__"* error. |
| 167 | + |
| 168 | +In order to solve this issue, add the following configuration to your kernel: |
| 169 | + |
| 170 | +.. code-block:: yaml |
| 171 | +
|
| 172 | + # api/config/config.yml |
| 173 | + twig: |
| 174 | + paths: |
| 175 | + # allows to use app/Resources/views/ templates in the ApiKernel |
| 176 | + "%kernel.root_dir%/../app/Resources/views": ~ |
| 177 | +
|
| 178 | +Adding more Kernels to the Application |
| 179 | +-------------------------------------- |
| 180 | + |
| 181 | +If your application is very complex and you create several kernels, it's better |
| 182 | +to store them in their own directories instead of messing with lots of files in |
| 183 | +the default ``app/`` directory: |
| 184 | + |
| 185 | +.. code-block:: text |
| 186 | +
|
| 187 | + project/ |
| 188 | + ├─ app/ |
| 189 | + │ ├─ ... |
| 190 | + │ ├─ config/ |
| 191 | + │ └─ AppKernel.php |
| 192 | + ├─ api/ |
| 193 | + │ ├─ ... |
| 194 | + │ ├─ config/ |
| 195 | + │ └─ ApiKernel.php |
| 196 | + ├─ ... |
| 197 | + └─ web/ |
| 198 | + ├─ ... |
| 199 | + ├─ app.php |
| 200 | + ├─ app_dev.php |
| 201 | + ├─ api.php |
| 202 | + └─ api_dev.php |
0 commit comments