@@ -16,9 +16,7 @@ via Composer:
1616
1717.. code-block :: terminal
1818
19- $ composer require symfony/config symfony/http-kernel \
20- symfony/http-foundation symfony/routing \
21- symfony/dependency-injection symfony/framework-bundle
19+ $ composer symfony/framework-bundle symfony/runtime
2220
2321 Next, create an ``index.php `` file that defines the kernel class and runs it:
2422
@@ -34,19 +32,12 @@ Next, create an ``index.php`` file that defines the kernel class and runs it:
3432 use Symfony\Component\HttpKernel\Kernel as BaseKernel;
3533 use Symfony\Component\Routing\Attribute\Route;
3634
37- require __DIR__.'/vendor/autoload .php';
35+ require_once dirname( __DIR__) .'/vendor/autoload_runtime .php';
3836
3937 class Kernel extends BaseKernel
4038 {
4139 use MicroKernelTrait;
4240
43- public function registerBundles(): array
44- {
45- return [
46- new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
47- ];
48- }
49-
5041 protected function configureContainer(ContainerConfigurator $container): void
5142 {
5243 // PHP equivalent of config/packages/framework.yaml
@@ -64,11 +55,9 @@ Next, create an ``index.php`` file that defines the kernel class and runs it:
6455 }
6556 }
6657
67- $kernel = new Kernel('dev', true);
68- $request = Request::createFromGlobals();
69- $response = $kernel->handle($request);
70- $response->send();
71- $kernel->terminate($request, $response);
58+ return static function (array $context) {
59+ return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
60+ }
7261
7362 .. code-block :: php
7463
@@ -80,19 +69,12 @@ Next, create an ``index.php`` file that defines the kernel class and runs it:
8069 use Symfony\Component\HttpKernel\Kernel as BaseKernel;
8170 use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
8271
83- require __DIR__.'/vendor/autoload .php';
72+ require_once dirname( __DIR__) .'/vendor/autoload_runtime .php';
8473
8574 class Kernel extends BaseKernel
8675 {
8776 use MicroKernelTrait;
8877
89- public function registerBundles(): array
90- {
91- return [
92- new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
93- ];
94- }
95-
9678 protected function configureContainer(ContainerConfigurator $container): void
9779 {
9880 // PHP equivalent of config/packages/framework.yaml
@@ -114,17 +96,9 @@ Next, create an ``index.php`` file that defines the kernel class and runs it:
11496 }
11597 }
11698
117- $kernel = new Kernel('dev', true);
118- $request = Request::createFromGlobals();
119- $response = $kernel->handle($request);
120- $response->send();
121- $kernel->terminate($request, $response);
122-
123- .. note ::
124-
125- In addition to the ``index.php `` file, you'll need to create a directory called
126- ``config/ `` in your project (even if it's empty because you define the configuration
127- options inside the ``configureContainer() `` method).
99+ return static function (array $context) {
100+ return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
101+ }
128102
129103 That's it! To test it, start the :doc: `Symfony Local Web Server
130104</setup/symfony_server>`:
@@ -135,14 +109,50 @@ That's it! To test it, start the :doc:`Symfony Local Web Server
135109
136110 Then see the JSON response in your browser: http://localhost:8000/random/10
137111
112+ .. tip ::
113+
114+ If your kernel only defines a single controller, you can use an invokable method::
115+
116+ class Kernel extends BaseKernel
117+ {
118+ use MicroKernelTrait;
119+
120+ // ...
121+
122+ #[Route('/random/{limit}', name: 'random_number')]
123+ public function __invoke(int $limit): JsonResponse
124+ {
125+ // ...
126+ }
127+ }
128+
138129The Methods of a "Micro" Kernel
139130-------------------------------
140131
141132When you use the ``MicroKernelTrait ``, your kernel needs to have exactly three methods
142133that define your bundles, your services and your routes:
143134
144135**registerBundles() **
145- This is the same ``registerBundles() `` that you see in a normal kernel.
136+ This is the same ``registerBundles() `` that you see in a normal kernel. By
137+ default, the micro kernel only registers the ``FrameworkBundle ``. If you need
138+ to register more bundles, override this method::
139+
140+ use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
141+ use Symfony\Bundle\TwigBundle\TwigBundle;
142+ // ...
143+
144+ class Kernel extends BaseKernel
145+ {
146+ use MicroKernelTrait;
147+
148+ // ...
149+
150+ public function registerBundles(): array
151+ {
152+ yield new FrameworkBundle();
153+ yield new TwigBundle();
154+ }
155+ }
146156
147157**configureContainer(ContainerConfigurator $container) **
148158 This method builds and configures the container. In practice, you will use
@@ -151,9 +161,13 @@ that define your bundles, your services and your routes:
151161 services directly in PHP or load external configuration files (shown below).
152162
153163**configureRoutes(RoutingConfigurator $routes) **
154- Your job in this method is to add routes to the application. The
155- ``RoutingConfigurator `` has methods that make adding routes in PHP more
156- fun. You can also load external routing files (shown below).
164+ In this method, you can use the ``RoutingConfigurator `` object to define routes
165+ in your application and associate them to the controllers defined in this very
166+ same file.
167+
168+ However, it's more convenient to define the controller routes using PHP attributes,
169+ as shown above. That's why this method is commonly used only to load external
170+ routing files (e.g. from bundles) as shown below.
157171
158172Adding Interfaces to "Micro" Kernel
159173-----------------------------------
@@ -231,7 +245,10 @@ Now it looks like this::
231245 namespace App;
232246
233247 use App\DependencyInjection\AppExtension;
248+ use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
234249 use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
250+ use Symfony\Bundle\TwigBundle\TwigBundle;
251+ use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
235252 use Symfony\Component\DependencyInjection\ContainerBuilder;
236253 use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
237254 use Symfony\Component\HttpKernel\Kernel as BaseKernel;
@@ -241,18 +258,14 @@ Now it looks like this::
241258 {
242259 use MicroKernelTrait;
243260
244- public function registerBundles(): array
261+ public function registerBundles(): iterable
245262 {
246- $bundles = [
247- new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
248- new \Symfony\Bundle\TwigBundle\TwigBundle(),
249- ];
263+ yield FrameworkBundle();
264+ yield TwigBundle();
250265
251266 if ('dev' === $this->getEnvironment()) {
252- $bundles[] = new \Symfony\Bundle\WebProfilerBundle\ WebProfilerBundle();
267+ yield WebProfilerBundle();
253268 }
254-
255- return $bundles;
256269 }
257270
258271 protected function build(ContainerBuilder $containerBuilder): void
0 commit comments