@@ -20,55 +20,109 @@ via Composer:
20
20
symfony/http-foundation symfony/routing \
21
21
symfony/dependency-injection symfony/framework-bundle
22
22
23
- Next, create an ``index.php `` file that defines the kernel class and runs it::
23
+ Next, create an ``index.php `` file that defines the kernel class and runs it:
24
24
25
- // index.php
26
- use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
27
- use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
28
- use Symfony\Component\HttpFoundation\JsonResponse;
29
- use Symfony\Component\HttpFoundation\Request;
30
- use Symfony\Component\HttpKernel\Kernel as BaseKernel;
31
- use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
25
+ .. configuration-block ::
32
26
33
- require __DIR__.'/vendor/autoload. php';
27
+ .. code-block :: php-attributes
34
28
35
- class Kernel extends BaseKernel
36
- {
37
- use MicroKernelTrait;
29
+ // index.php
30
+ use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
31
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
32
+ use Symfony\Component\HttpFoundation\JsonResponse;
33
+ use Symfony\Component\HttpFoundation\Request;
34
+ use Symfony\Component\HttpKernel\Kernel as BaseKernel;
35
+ use Symfony\Component\Routing\Annotation\Route;
38
36
39
- public function registerBundles(): array
40
- {
41
- return [
42
- new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
43
- ];
44
- }
37
+ require __DIR__.'/vendor/autoload.php';
45
38
46
- protected function configureContainer(ContainerConfigurator $c): void
39
+ class Kernel extends BaseKernel
47
40
{
48
- // PHP equivalent of config/packages/framework.yaml
49
- $c->extension('framework', [
50
- 'secret' => 'S0ME_SECRET'
51
- ]);
52
- }
41
+ use MicroKernelTrait;
53
42
54
- protected function configureRoutes(RoutingConfigurator $routes): void
55
- {
56
- $routes->add('random_number', '/random/{limit}')->controller([$this, 'randomNumber']);
43
+ public function registerBundles(): array
44
+ {
45
+ return [
46
+ new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
47
+ ];
48
+ }
49
+
50
+ protected function configureContainer(ContainerConfigurator $c): void
51
+ {
52
+ // PHP equivalent of config/packages/framework.yaml
53
+ $c->extension('framework', [
54
+ 'secret' => 'S0ME_SECRET'
55
+ ]);
56
+ }
57
+
58
+ #[Route('/random/{limit}', name='random_number')]
59
+ public function randomNumber(int $limit): JsonResponse
60
+ {
61
+ return new JsonResponse([
62
+ 'number' => random_int(0, $limit),
63
+ ]);
64
+ }
57
65
}
58
66
59
- public function randomNumber(int $limit): JsonResponse
67
+ $kernel = new Kernel('dev', true);
68
+ $request = Request::createFromGlobals();
69
+ $response = $kernel->handle($request);
70
+ $response->send();
71
+ $kernel->terminate($request, $response);
72
+
73
+ .. code-block :: php
74
+
75
+ // index.php
76
+ use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
77
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
78
+ use Symfony\Component\HttpFoundation\JsonResponse;
79
+ use Symfony\Component\HttpFoundation\Request;
80
+ use Symfony\Component\HttpKernel\Kernel as BaseKernel;
81
+ use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
82
+
83
+ require __DIR__.'/vendor/autoload.php';
84
+
85
+ class Kernel extends BaseKernel
60
86
{
61
- return new JsonResponse([
62
- 'number' => random_int(0, $limit),
63
- ]);
87
+ use MicroKernelTrait;
88
+
89
+ public function registerBundles(): array
90
+ {
91
+ return [
92
+ new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
93
+ ];
94
+ }
95
+
96
+ protected function configureContainer(ContainerConfigurator $c): void
97
+ {
98
+ // PHP equivalent of config/packages/framework.yaml
99
+ $c->extension('framework', [
100
+ 'secret' => 'S0ME_SECRET'
101
+ ]);
102
+ }
103
+
104
+ protected function configureRoutes(RoutingConfigurator $routes): void
105
+ {
106
+ $routes->add('random_number', '/random/{limit}')->controller([$this, 'randomNumber']);
107
+ }
108
+
109
+ public function randomNumber(int $limit): JsonResponse
110
+ {
111
+ return new JsonResponse([
112
+ 'number' => random_int(0, $limit),
113
+ ]);
114
+ }
64
115
}
65
- }
66
116
67
- $kernel = new Kernel('dev', true);
68
- $request = Request::createFromGlobals();
69
- $response = $kernel->handle($request);
70
- $response->send();
71
- $kernel->terminate($request, $response);
117
+ $kernel = new Kernel('dev', true);
118
+ $request = Request::createFromGlobals();
119
+ $response = $kernel->handle($request);
120
+ $response->send();
121
+ $kernel->terminate($request, $response);
122
+
123
+ .. versionadded :: 6.1
124
+
125
+ The PHP attributes notation has been introduced in Symfony 6.1.
72
126
73
127
That's it! To test it, start the :doc: `Symfony Local Web Server
74
128
</setup/symfony_server>`:
0 commit comments