@@ -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,27 +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
56
$routes->add('random_number', '/random/{limit}')->controller([$this, 'randomNumber']);
58
57
}
59
58
60
- public function randomNumber($limit)
59
+ public function randomNumber(int $limit): JsonResponse
61
60
{
62
61
return new JsonResponse([
63
62
'number' => random_int(0, $limit),
@@ -89,9 +88,9 @@ that define your bundles, your services and your routes:
89
88
**registerBundles() **
90
89
This is the same ``registerBundles() `` that you see in a normal kernel.
91
90
92
- **configureContainer(ContainerBuilder $c, LoaderInterface $loader ) **
91
+ **configureContainer(ContainerConfigurator $c) **
93
92
This method builds and configures the container. In practice, you will use
94
- ``loadFromExtension `` to configure different bundles (this is the equivalent
93
+ ``extension() `` to configure different bundles (this is the equivalent
95
94
of what you see in a normal ``config/packages/* `` file). You can also register
96
95
services directly in PHP or load external configuration files (shown below).
97
96
@@ -132,16 +131,15 @@ hold the kernel. Now it looks like this::
132
131
namespace App;
133
132
134
133
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
135
- use Symfony\Component\Config\Loader\LoaderInterface;
136
- use Symfony\Component\DependencyInjection\ContainerBuilder;
134
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
137
135
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
138
136
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
139
137
140
138
class Kernel extends BaseKernel
141
139
{
142
140
use MicroKernelTrait;
143
141
144
- public function registerBundles()
142
+ public function registerBundles(): array
145
143
{
146
144
$bundles = [
147
145
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
@@ -155,21 +153,27 @@ hold the kernel. Now it looks like this::
155
153
return $bundles;
156
154
}
157
155
158
- protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
156
+ protected function configureContainer(ContainerConfigurator $c): void
159
157
{
160
- $loader->load(__DIR__.'/../config/framework.yaml');
161
- $loader->load(__DIR__.'/../config/services.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
+ ;
162
166
163
167
// configure WebProfilerBundle only if the bundle is enabled
164
168
if (isset($this->bundles['WebProfilerBundle'])) {
165
- $c->loadFromExtension ('web_profiler', [
169
+ $c->extension ('web_profiler', [
166
170
'toolbar' => true,
167
171
'intercept_redirects' => false,
168
172
]);
169
173
}
170
174
}
171
175
172
- protected function configureRoutes(RoutingConfigurator $routes)
176
+ protected function configureRoutes(RoutingConfigurator $routes): void
173
177
{
174
178
// import the WebProfilerRoutes, only if the bundle is enabled
175
179
if (isset($this->bundles['WebProfilerBundle'])) {
@@ -178,17 +182,17 @@ hold the kernel. Now it looks like this::
178
182
}
179
183
180
184
// load the annotation routes
181
- $routes->import(__DIR__.'/../src/ Controller/', 'annotation');
185
+ $routes->import(__DIR__.'/Controller/', 'annotation');
182
186
}
183
187
184
188
// optional, to use the standard Symfony cache directory
185
- public function getCacheDir()
189
+ public function getCacheDir(): string
186
190
{
187
191
return __DIR__.'/../var/cache/'.$this->getEnvironment();
188
192
}
189
193
190
194
// optional, to use the standard Symfony logs directory
191
- public function getLogDir()
195
+ public function getLogDir(): string
192
196
{
193
197
return __DIR__.'/../var/log';
194
198
}
@@ -200,61 +204,6 @@ Before continuing, run this command to add support for the new dependencies:
200
204
201
205
$ composer require symfony/yaml symfony/twig-bundle symfony/web-profiler-bundle doctrine/annotations
202
206
203
- You need add the following service configuration, which is the default config for a new project:
204
-
205
- .. configuration-block ::
206
-
207
- .. code-block :: yaml
208
-
209
- # config/services.yaml
210
- services :
211
- # default configuration for services in *this* file
212
- _defaults :
213
- autowire : true # Automatically injects dependencies in your services.
214
- autoconfigure : true # Automatically registers your services as commands, event subscribers, etc.
215
-
216
- # makes classes in src/ available to be used as services
217
- # this creates a service per class whose id is the fully-qualified class name
218
- App\ :
219
- resource : ' ../src/*'
220
-
221
- .. code-block :: xml
222
-
223
- <!-- config/services.xml -->
224
- <?xml version =" 1.0" encoding =" UTF-8" ?>
225
- <container xmlns =" http://symfony.com/schema/dic/services"
226
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
227
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
228
- https://symfony.com/schema/dic/services/services-1.0.xsd" >
229
-
230
- <services >
231
- <!-- Default configuration for services in *this* file -->
232
- <defaults autowire =" true" autoconfigure =" true" />
233
-
234
- <!-- makes classes in src/ available to be used as services -->
235
- <!-- this creates a service per class whose id is the fully-qualified class name -->
236
- <prototype namespace =" App\" resource =" ../src/*" />
237
- </services >
238
- </container >
239
-
240
- .. code-block :: php
241
-
242
- // config/services.php
243
- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
244
-
245
- return function(ContainerConfigurator $configurator) {
246
- // default configuration for services in *this* file
247
- $services = $configurator->services()
248
- ->defaults()
249
- ->autowire() // Automatically injects dependencies in your services.
250
- ->autoconfigure() // Automatically registers your services as commands, event subscribers, etc.
251
- ;
252
-
253
- // makes classes in src/ available to be used as services
254
- // this creates a service per class whose id is the fully-qualified class name
255
- $services->load('App\\', '../src/*');
256
- };
257
-
258
207
Unlike the previous kernel, this loads an external ``config/framework.yaml `` file,
259
208
because the configuration started to get bigger:
260
209
@@ -299,14 +248,15 @@ has one file in it::
299
248
namespace App\Controller;
300
249
301
250
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
251
+ use Symfony\Component\HttpFoundation\Response;
302
252
use Symfony\Component\Routing\Annotation\Route;
303
253
304
254
class MicroController extends AbstractController
305
255
{
306
256
/**
307
257
* @Route("/random/{limit}")
308
258
*/
309
- public function randomNumber($limit)
259
+ public function randomNumber(int $limit): Response
310
260
{
311
261
$number = random_int(0, $limit);
312
262
@@ -381,7 +331,6 @@ As before you can use the :doc:`Symfony Local Web Server
381
331
382
332
.. code-block :: terminal
383
333
384
- cd public/
385
334
$ symfony server:start
386
335
387
336
Then visit the page in your browser: http://localhost:8000/random/10
0 commit comments