@@ -24,8 +24,7 @@ Next, create an ``index.php`` file that defines the kernel class and executes it
24
24
25
25
// index.php
26
26
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
27
- use Symfony\Component\Config\Loader\LoaderInterface;
28
- use Symfony\Component\DependencyInjection\ContainerBuilder;
27
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
29
28
use Symfony\Component\HttpFoundation\JsonResponse;
30
29
use Symfony\Component\HttpFoundation\Request;
31
30
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
@@ -37,29 +36,27 @@ Next, create an ``index.php`` file that defines the kernel class and executes it
37
36
{
38
37
use MicroKernelTrait;
39
38
40
- public function registerBundles()
39
+ public function registerBundles(): array
41
40
{
42
41
return [
43
- new Symfony\Bundle\FrameworkBundle\FrameworkBundle()
42
+ new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
44
43
];
45
44
}
46
45
47
- protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
46
+ protected function configureContainer(ContainerConfigurator $c): void
48
47
{
49
48
// PHP equivalent of config/packages/framework.yaml
50
- $c->loadFromExtension ('framework', [
49
+ $c->extension ('framework', [
51
50
'secret' => 'S0ME_SECRET'
52
51
]);
53
52
}
54
53
55
- protected function configureRoutes(RoutingConfigurator $routes)
54
+ protected function configureRoutes(RoutingConfigurator $routes): void
56
55
{
57
- // kernel is a service that points to this class
58
- // optional 3rd argument is the route name
59
- $routes->add('/random/{limit}', 'kernel::randomNumber');
56
+ $routes->add('random_number', '/random/{limit}')->controller([$this, 'randomNumber']);
60
57
}
61
58
62
- public function randomNumber($limit)
59
+ public function randomNumber(int $limit): JsonResponse
63
60
{
64
61
return new JsonResponse([
65
62
'number' => random_int(0, $limit),
@@ -91,9 +88,9 @@ that define your bundles, your services and your routes:
91
88
**registerBundles() **
92
89
This is the same ``registerBundles() `` that you see in a normal kernel.
93
90
94
- **configureContainer(ContainerBuilder $c, LoaderInterface $loader ) **
91
+ **configureContainer(ContainerConfigurator $c) **
95
92
This method builds and configures the container. In practice, you will use
96
- ``loadFromExtension `` to configure different bundles (this is the equivalent
93
+ ``extension() `` to configure different bundles (this is the equivalent
97
94
of what you see in a normal ``config/packages/* `` file). You can also register
98
95
services directly in PHP or load external configuration files (shown below).
99
96
@@ -134,43 +131,49 @@ hold the kernel. Now it looks like this::
134
131
namespace App;
135
132
136
133
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
137
- use Symfony\Component\Config\Loader\LoaderInterface;
138
- use Symfony\Component\DependencyInjection\ContainerBuilder;
134
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
139
135
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
140
136
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
141
137
142
138
class Kernel extends BaseKernel
143
139
{
144
140
use MicroKernelTrait;
145
141
146
- public function registerBundles()
142
+ public function registerBundles(): array
147
143
{
148
144
$bundles = [
149
145
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
150
146
new \Symfony\Bundle\TwigBundle\TwigBundle(),
151
147
];
152
148
153
149
if ($this->getEnvironment() == 'dev') {
154
- $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
150
+ $bundles[] = new \ Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
155
151
}
156
152
157
153
return $bundles;
158
154
}
159
155
160
- protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
156
+ protected function configureContainer(ContainerConfigurator $c): void
161
157
{
162
- $loader->load(__DIR__.'/../config/framework.yaml');
158
+ $c->import(__DIR__.'/../config/framework.yaml');
159
+
160
+ // register all classes in /src/ as service
161
+ $c->services()
162
+ ->load('App\\', __DIR__.'/*')
163
+ ->autowire()
164
+ ->autoconfigure()
165
+ ;
163
166
164
167
// configure WebProfilerBundle only if the bundle is enabled
165
168
if (isset($this->bundles['WebProfilerBundle'])) {
166
- $c->loadFromExtension ('web_profiler', [
169
+ $c->extension ('web_profiler', [
167
170
'toolbar' => true,
168
171
'intercept_redirects' => false,
169
172
]);
170
173
}
171
174
}
172
175
173
- protected function configureRoutes(RoutingConfigurator $routes)
176
+ protected function configureRoutes(RoutingConfigurator $routes): void
174
177
{
175
178
// import the WebProfilerRoutes, only if the bundle is enabled
176
179
if (isset($this->bundles['WebProfilerBundle'])) {
@@ -179,17 +182,17 @@ hold the kernel. Now it looks like this::
179
182
}
180
183
181
184
// load the annotation routes
182
- $routes->import(__DIR__.'/../src/ Controller/', 'annotation');
185
+ $routes->import(__DIR__.'/Controller/', 'annotation');
183
186
}
184
187
185
188
// optional, to use the standard Symfony cache directory
186
- public function getCacheDir()
189
+ public function getCacheDir(): string
187
190
{
188
191
return __DIR__.'/../var/cache/'.$this->getEnvironment();
189
192
}
190
193
191
194
// optional, to use the standard Symfony logs directory
192
- public function getLogDir()
195
+ public function getLogDir(): string
193
196
{
194
197
return __DIR__.'/../var/log';
195
198
}
@@ -245,14 +248,15 @@ has one file in it::
245
248
namespace App\Controller;
246
249
247
250
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
251
+ use Symfony\Component\HttpFoundation\Response;
248
252
use Symfony\Component\Routing\Annotation\Route;
249
253
250
254
class MicroController extends AbstractController
251
255
{
252
256
/**
253
257
* @Route("/random/{limit}")
254
258
*/
255
- public function randomNumber($limit)
259
+ public function randomNumber(int $limit): Response
256
260
{
257
261
$number = random_int(0, $limit);
258
262
@@ -327,7 +331,6 @@ As before you can use the :doc:`Symfony Local Web Server
327
331
328
332
.. code-block :: terminal
329
333
330
- cd public/
331
334
$ symfony server:start
332
335
333
336
Then visit the page in your browser: http://localhost:8000/random/10
0 commit comments