Skip to content

Commit a8c81fe

Browse files
[FrameworkBundle] Add support for route attributes in kernel controller methods
1 parent cf1b86e commit a8c81fe

File tree

1 file changed

+91
-37
lines changed

1 file changed

+91
-37
lines changed

configuration/micro_kernel_trait.rst

Lines changed: 91 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,109 @@ via Composer:
2020
symfony/http-foundation symfony/routing \
2121
symfony/dependency-injection symfony/framework-bundle
2222
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:
2424

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::
3226

33-
require __DIR__.'/vendor/autoload.php';
27+
.. code-block:: php
3428
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\Loader\Configurator\RoutingConfigurator;
3836
39-
public function registerBundles(): array
40-
{
41-
return [
42-
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
43-
];
44-
}
37+
require __DIR__.'/vendor/autoload.php';
4538
46-
protected function configureContainer(ContainerConfigurator $c): void
39+
class Kernel extends BaseKernel
4740
{
48-
// PHP equivalent of config/packages/framework.yaml
49-
$c->extension('framework', [
50-
'secret' => 'S0ME_SECRET'
51-
]);
52-
}
41+
use MicroKernelTrait;
5342
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+
protected function configureRoutes(RoutingConfigurator $routes): void
59+
{
60+
$routes->add('random_number', '/random/{limit}')->controller([$this, 'randomNumber']);
61+
}
62+
63+
public function randomNumber(int $limit): JsonResponse
64+
{
65+
return new JsonResponse([
66+
'number' => random_int(0, $limit),
67+
]);
68+
}
5769
}
5870
59-
public function randomNumber(int $limit): JsonResponse
71+
$kernel = new Kernel('dev', true);
72+
$request = Request::createFromGlobals();
73+
$response = $kernel->handle($request);
74+
$response->send();
75+
$kernel->terminate($request, $response);
76+
77+
.. code-block:: php-attributes
78+
79+
// index.php
80+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
81+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
82+
use Symfony\Component\HttpFoundation\JsonResponse;
83+
use Symfony\Component\HttpFoundation\Request;
84+
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
85+
use Symfony\Component\Routing\Annotation\Route;
86+
87+
require __DIR__.'/vendor/autoload.php';
88+
89+
class Kernel extends BaseKernel
6090
{
61-
return new JsonResponse([
62-
'number' => random_int(0, $limit),
63-
]);
91+
use MicroKernelTrait;
92+
93+
public function registerBundles(): array
94+
{
95+
return [
96+
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
97+
];
98+
}
99+
100+
protected function configureContainer(ContainerConfigurator $c): void
101+
{
102+
// PHP equivalent of config/packages/framework.yaml
103+
$c->extension('framework', [
104+
'secret' => 'S0ME_SECRET'
105+
]);
106+
}
107+
108+
#[Route('/random/{limit}', name='random_number')]
109+
public function randomNumber(int $limit): JsonResponse
110+
{
111+
return new JsonResponse([
112+
'number' => random_int(0, $limit),
113+
]);
114+
}
64115
}
65-
}
66116
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.
72126

73127
That's it! To test it, start the :doc:`Symfony Local Web Server
74128
</setup/symfony_server>`:

0 commit comments

Comments
 (0)