@@ -40,13 +40,6 @@ Next, create an ``index.php`` file that defines the kernel class and runs it:
4040 {
4141 use MicroKernelTrait;
4242
43- public function registerBundles(): array
44- {
45- return [
46- new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
47- ];
48- }
49-
5043 protected function configureContainer(ContainerConfigurator $container): void
5144 {
5245 // PHP equivalent of config/packages/framework.yaml
@@ -86,13 +79,6 @@ Next, create an ``index.php`` file that defines the kernel class and runs it:
8679 {
8780 use MicroKernelTrait;
8881
89- public function registerBundles(): array
90- {
91- return [
92- new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
93- ];
94- }
95-
9682 protected function configureContainer(ContainerConfigurator $container): void
9783 {
9884 // PHP equivalent of config/packages/framework.yaml
@@ -120,12 +106,6 @@ Next, create an ``index.php`` file that defines the kernel class and runs it:
120106 $response->send();
121107 $kernel->terminate($request, $response);
122108
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).
128-
129109 That's it! To test it, start the :doc: `Symfony Local Web Server
130110</setup/symfony_server>`:
131111
@@ -135,14 +115,50 @@ That's it! To test it, start the :doc:`Symfony Local Web Server
135115
136116 Then see the JSON response in your browser: http://localhost:8000/random/10
137117
118+ .. tip ::
119+
120+ If your kernel only defines a single controller, you can use an invokable method::
121+
122+ class Kernel extends BaseKernel
123+ {
124+ use MicroKernelTrait;
125+
126+ // ...
127+
128+ #[Route('/random/{limit}', name: 'random_number')]
129+ public function __invoke(int $limit): JsonResponse
130+ {
131+ // ...
132+ }
133+ }
134+
138135The Methods of a "Micro" Kernel
139136-------------------------------
140137
141138When you use the ``MicroKernelTrait ``, your kernel needs to have exactly three methods
142139that define your bundles, your services and your routes:
143140
144141**registerBundles() **
145- This is the same ``registerBundles() `` that you see in a normal kernel.
142+ This is the same ``registerBundles() `` that you see in a normal kernel. By
143+ default, the micro kernel only registers the ``FrameworkBundle ``. If you need
144+ to register more bundles, override this method::
145+
146+ use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
147+ use Symfony\Bundle\TwigBundle\TwigBundle;
148+ // ...
149+
150+ class Kernel extends BaseKernel
151+ {
152+ use MicroKernelTrait;
153+
154+ // ...
155+
156+ public function registerBundles(): array
157+ {
158+ yield new FrameworkBundle();
159+ yield new TwigBundle();
160+ }
161+ }
146162
147163**configureContainer(ContainerConfigurator $container) **
148164 This method builds and configures the container. In practice, you will use
@@ -151,9 +167,13 @@ that define your bundles, your services and your routes:
151167 services directly in PHP or load external configuration files (shown below).
152168
153169**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).
170+ In this method, you can use the ``RoutingConfigurator `` object to define routes
171+ in your application and associate them to the controllers defined in this very
172+ same file.
173+
174+ However, it's more convenient to define the controller routes using PHP attributes,
175+ as shown above. That's why this method is commonly used only to load external
176+ routing files (e.g. from bundles) as shown below.
157177
158178Adding Interfaces to "Micro" Kernel
159179-----------------------------------
@@ -231,7 +251,10 @@ Now it looks like this::
231251 namespace App;
232252
233253 use App\DependencyInjection\AppExtension;
254+ use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
234255 use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
256+ use Symfony\Bundle\TwigBundle\TwigBundle;
257+ use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
235258 use Symfony\Component\DependencyInjection\ContainerBuilder;
236259 use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
237260 use Symfony\Component\HttpKernel\Kernel as BaseKernel;
@@ -241,18 +264,14 @@ Now it looks like this::
241264 {
242265 use MicroKernelTrait;
243266
244- public function registerBundles(): array
267+ public function registerBundles(): iterable
245268 {
246- $bundles = [
247- new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
248- new \Symfony\Bundle\TwigBundle\TwigBundle(),
249- ];
269+ yield FrameworkBundle();
270+ yield TwigBundle();
250271
251272 if ('dev' === $this->getEnvironment()) {
252- $bundles[] = new \Symfony\Bundle\WebProfilerBundle\ WebProfilerBundle();
273+ yield WebProfilerBundle();
253274 }
254-
255- return $bundles;
256275 }
257276
258277 protected function build(ContainerBuilder $containerBuilder): void
0 commit comments