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 e1c35a5

Browse filesBrowse files
committed
[#13919] Use ContainerConfigurator and fixes some bugs after testing the example code
1 parent 6d1ec6b commit e1c35a5
Copy full SHA for e1c35a5

File tree

1 file changed

+27
-78
lines changed
Filter options

1 file changed

+27
-78
lines changed

‎configuration/micro_kernel_trait.rst

Copy file name to clipboardExpand all lines: configuration/micro_kernel_trait.rst
+27-78Lines changed: 27 additions & 78 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,27 +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
{
5756
$routes->add('random_number', '/random/{limit}')->controller([$this, 'randomNumber']);
5857
}
5958

60-
public function randomNumber($limit)
59+
public function randomNumber(int $limit): JsonResponse
6160
{
6261
return new JsonResponse([
6362
'number' => random_int(0, $limit),
@@ -89,9 +88,9 @@ that define your bundles, your services and your routes:
8988
**registerBundles()**
9089
This is the same ``registerBundles()`` that you see in a normal kernel.
9190

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

@@ -132,16 +131,15 @@ hold the kernel. Now it looks like this::
132131
namespace App;
133132

134133
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;
137135
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
138136
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
139137

140138
class Kernel extends BaseKernel
141139
{
142140
use MicroKernelTrait;
143141

144-
public function registerBundles()
142+
public function registerBundles(): array
145143
{
146144
$bundles = [
147145
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
@@ -155,21 +153,27 @@ hold the kernel. Now it looks like this::
155153
return $bundles;
156154
}
157155

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

163167
// configure WebProfilerBundle only if the bundle is enabled
164168
if (isset($this->bundles['WebProfilerBundle'])) {
165-
$c->loadFromExtension('web_profiler', [
169+
$c->extension('web_profiler', [
166170
'toolbar' => true,
167171
'intercept_redirects' => false,
168172
]);
169173
}
170174
}
171175

172-
protected function configureRoutes(RoutingConfigurator $routes)
176+
protected function configureRoutes(RoutingConfigurator $routes): void
173177
{
174178
// import the WebProfilerRoutes, only if the bundle is enabled
175179
if (isset($this->bundles['WebProfilerBundle'])) {
@@ -178,17 +182,17 @@ hold the kernel. Now it looks like this::
178182
}
179183

180184
// load the annotation routes
181-
$routes->import(__DIR__.'/../src/Controller/', 'annotation');
185+
$routes->import(__DIR__.'/Controller/', 'annotation');
182186
}
183187

184188
// optional, to use the standard Symfony cache directory
185-
public function getCacheDir()
189+
public function getCacheDir(): string
186190
{
187191
return __DIR__.'/../var/cache/'.$this->getEnvironment();
188192
}
189193

190194
// optional, to use the standard Symfony logs directory
191-
public function getLogDir()
195+
public function getLogDir(): string
192196
{
193197
return __DIR__.'/../var/log';
194198
}
@@ -200,61 +204,6 @@ Before continuing, run this command to add support for the new dependencies:
200204
201205
$ composer require symfony/yaml symfony/twig-bundle symfony/web-profiler-bundle doctrine/annotations
202206
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-
258207
Unlike the previous kernel, this loads an external ``config/framework.yaml`` file,
259208
because the configuration started to get bigger:
260209

@@ -299,14 +248,15 @@ has one file in it::
299248
namespace App\Controller;
300249

301250
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
251+
use Symfony\Component\HttpFoundation\Response;
302252
use Symfony\Component\Routing\Annotation\Route;
303253

304254
class MicroController extends AbstractController
305255
{
306256
/**
307257
* @Route("/random/{limit}")
308258
*/
309-
public function randomNumber($limit)
259+
public function randomNumber(int $limit): Response
310260
{
311261
$number = random_int(0, $limit);
312262

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

382332
.. code-block:: terminal
383333
384-
cd public/
385334
$ symfony server:start
386335
387336
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.