@@ -40,13 +40,6 @@ Next, create an ``index.php`` file that defines the kernel class and runs it:
40
40
{
41
41
use MicroKernelTrait;
42
42
43
- public function registerBundles(): array
44
- {
45
- return [
46
- new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
47
- ];
48
- }
49
-
50
43
protected function configureContainer(ContainerConfigurator $container): void
51
44
{
52
45
// 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:
86
79
{
87
80
use MicroKernelTrait;
88
81
89
- public function registerBundles(): array
90
- {
91
- return [
92
- new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
93
- ];
94
- }
95
-
96
82
protected function configureContainer(ContainerConfigurator $container): void
97
83
{
98
84
// 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:
120
106
$response->send();
121
107
$kernel->terminate($request, $response);
122
108
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
-
129
109
That's it! To test it, start the :doc: `Symfony Local Web Server
130
110
</setup/symfony_server>`:
131
111
@@ -135,14 +115,50 @@ That's it! To test it, start the :doc:`Symfony Local Web Server
135
115
136
116
Then see the JSON response in your browser: http://localhost:8000/random/10
137
117
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
+
138
135
The Methods of a "Micro" Kernel
139
136
-------------------------------
140
137
141
138
When you use the ``MicroKernelTrait ``, your kernel needs to have exactly three methods
142
139
that define your bundles, your services and your routes:
143
140
144
141
**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
+ }
146
162
147
163
**configureContainer(ContainerConfigurator $container) **
148
164
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:
151
167
services directly in PHP or load external configuration files (shown below).
152
168
153
169
**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.
157
177
158
178
Adding Interfaces to "Micro" Kernel
159
179
-----------------------------------
@@ -231,7 +251,10 @@ Now it looks like this::
231
251
namespace App;
232
252
233
253
use App\DependencyInjection\AppExtension;
254
+ use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
234
255
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
256
+ use Symfony\Bundle\TwigBundle\TwigBundle;
257
+ use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
235
258
use Symfony\Component\DependencyInjection\ContainerBuilder;
236
259
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
237
260
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
@@ -241,18 +264,14 @@ Now it looks like this::
241
264
{
242
265
use MicroKernelTrait;
243
266
244
- public function registerBundles(): array
267
+ public function registerBundles(): iterable
245
268
{
246
- $bundles = [
247
- new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
248
- new \Symfony\Bundle\TwigBundle\TwigBundle(),
249
- ];
269
+ yield FrameworkBundle();
270
+ yield TwigBundle();
250
271
251
272
if ('dev' === $this->getEnvironment()) {
252
- $bundles[] = new \Symfony\Bundle\WebProfilerBundle\ WebProfilerBundle();
273
+ yield WebProfilerBundle();
253
274
}
254
-
255
- return $bundles;
256
275
}
257
276
258
277
protected function build(ContainerBuilder $containerBuilder): void
0 commit comments