Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5888a81

Browse filesBrowse files
committed
Merge branch '5.1' of github.com:symfony/symfony-docs into 5.1
* '5.1' of github.com:symfony/symfony-docs: [#13919] Use ContainerConfigurator and fixes some bugs after testing the example code Fixed micro kernel demo
2 parents 8776726 + e1c35a5 commit 5888a81
Copy full SHA for 5888a81

File tree

1 file changed

+29
-26
lines changed
Filter options

1 file changed

+29
-26
lines changed

‎configuration/micro_kernel_trait.rst

Copy file name to clipboardExpand all lines: configuration/micro_kernel_trait.rst
+29-26Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ Next, create an ``index.php`` file that defines the kernel class and executes it
2424

2525
// index.php
2626
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;
2928
use Symfony\Component\HttpFoundation\JsonResponse;
3029
use Symfony\Component\HttpFoundation\Request;
3130
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
3736
{
3837
use MicroKernelTrait;
3938

40-
public function registerBundles()
39+
public function registerBundles(): array
4140
{
4241
return [
43-
new Symfony\Bundle\FrameworkBundle\FrameworkBundle()
42+
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
4443
];
4544
}
4645

47-
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
46+
protected function configureContainer(ContainerConfigurator $c): void
4847
{
4948
// PHP equivalent of config/packages/framework.yaml
50-
$c->loadFromExtension('framework', [
49+
$c->extension('framework', [
5150
'secret' => 'S0ME_SECRET'
5251
]);
5352
}
5453

55-
protected function configureRoutes(RoutingConfigurator $routes)
54+
protected function configureRoutes(RoutingConfigurator $routes): void
5655
{
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']);
6057
}
6158

62-
public function randomNumber($limit)
59+
public function randomNumber(int $limit): JsonResponse
6360
{
6461
return new JsonResponse([
6562
'number' => random_int(0, $limit),
@@ -91,9 +88,9 @@ that define your bundles, your services and your routes:
9188
**registerBundles()**
9289
This is the same ``registerBundles()`` that you see in a normal kernel.
9390

94-
**configureContainer(ContainerBuilder $c, LoaderInterface $loader)**
91+
**configureContainer(ContainerConfigurator $c)**
9592
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
9794
of what you see in a normal ``config/packages/*`` file). You can also register
9895
services directly in PHP or load external configuration files (shown below).
9996

@@ -134,43 +131,49 @@ hold the kernel. Now it looks like this::
134131
namespace App;
135132

136133
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;
139135
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
140136
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
141137

142138
class Kernel extends BaseKernel
143139
{
144140
use MicroKernelTrait;
145141

146-
public function registerBundles()
142+
public function registerBundles(): array
147143
{
148144
$bundles = [
149145
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
150146
new \Symfony\Bundle\TwigBundle\TwigBundle(),
151147
];
152148

153149
if ($this->getEnvironment() == 'dev') {
154-
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
150+
$bundles[] = new \Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
155151
}
156152

157153
return $bundles;
158154
}
159155

160-
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
156+
protected function configureContainer(ContainerConfigurator $c): void
161157
{
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+
;
163166

164167
// configure WebProfilerBundle only if the bundle is enabled
165168
if (isset($this->bundles['WebProfilerBundle'])) {
166-
$c->loadFromExtension('web_profiler', [
169+
$c->extension('web_profiler', [
167170
'toolbar' => true,
168171
'intercept_redirects' => false,
169172
]);
170173
}
171174
}
172175

173-
protected function configureRoutes(RoutingConfigurator $routes)
176+
protected function configureRoutes(RoutingConfigurator $routes): void
174177
{
175178
// import the WebProfilerRoutes, only if the bundle is enabled
176179
if (isset($this->bundles['WebProfilerBundle'])) {
@@ -179,17 +182,17 @@ hold the kernel. Now it looks like this::
179182
}
180183

181184
// load the annotation routes
182-
$routes->import(__DIR__.'/../src/Controller/', 'annotation');
185+
$routes->import(__DIR__.'/Controller/', 'annotation');
183186
}
184187

185188
// optional, to use the standard Symfony cache directory
186-
public function getCacheDir()
189+
public function getCacheDir(): string
187190
{
188191
return __DIR__.'/../var/cache/'.$this->getEnvironment();
189192
}
190193

191194
// optional, to use the standard Symfony logs directory
192-
public function getLogDir()
195+
public function getLogDir(): string
193196
{
194197
return __DIR__.'/../var/log';
195198
}
@@ -245,14 +248,15 @@ has one file in it::
245248
namespace App\Controller;
246249

247250
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
251+
use Symfony\Component\HttpFoundation\Response;
248252
use Symfony\Component\Routing\Annotation\Route;
249253

250254
class MicroController extends AbstractController
251255
{
252256
/**
253257
* @Route("/random/{limit}")
254258
*/
255-
public function randomNumber($limit)
259+
public function randomNumber(int $limit): Response
256260
{
257261
$number = random_int(0, $limit);
258262

@@ -327,7 +331,6 @@ As before you can use the :doc:`Symfony Local Web Server
327331

328332
.. code-block:: terminal
329333
330-
cd public/
331334
$ symfony server:start
332335
333336
Then visit the page in your browser: http://localhost:8000/random/10

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.