From a8c81fe9c12acff8d46f3a36cdfd74d73281190c Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Wed, 4 Jan 2023 13:24:40 +0100 Subject: [PATCH] [FrameworkBundle] Add support for route attributes in kernel controller methods --- configuration/micro_kernel_trait.rst | 128 +++++++++++++++++++-------- 1 file changed, 91 insertions(+), 37 deletions(-) diff --git a/configuration/micro_kernel_trait.rst b/configuration/micro_kernel_trait.rst index 3d479829a94..97781fa383c 100644 --- a/configuration/micro_kernel_trait.rst +++ b/configuration/micro_kernel_trait.rst @@ -20,55 +20,109 @@ via Composer: symfony/http-foundation symfony/routing \ symfony/dependency-injection symfony/framework-bundle -Next, create an ``index.php`` file that defines the kernel class and runs it:: +Next, create an ``index.php`` file that defines the kernel class and runs it: - // index.php - use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; - use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; - use Symfony\Component\HttpFoundation\JsonResponse; - use Symfony\Component\HttpFoundation\Request; - use Symfony\Component\HttpKernel\Kernel as BaseKernel; - use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; +.. configuration-block:: - require __DIR__.'/vendor/autoload.php'; + .. code-block:: php - class Kernel extends BaseKernel - { - use MicroKernelTrait; + // index.php + use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; + use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + use Symfony\Component\HttpFoundation\JsonResponse; + use Symfony\Component\HttpFoundation\Request; + use Symfony\Component\HttpKernel\Kernel as BaseKernel; + use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; - public function registerBundles(): array - { - return [ - new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), - ]; - } + require __DIR__.'/vendor/autoload.php'; - protected function configureContainer(ContainerConfigurator $c): void + class Kernel extends BaseKernel { - // PHP equivalent of config/packages/framework.yaml - $c->extension('framework', [ - 'secret' => 'S0ME_SECRET' - ]); - } + use MicroKernelTrait; - protected function configureRoutes(RoutingConfigurator $routes): void - { - $routes->add('random_number', '/random/{limit}')->controller([$this, 'randomNumber']); + public function registerBundles(): array + { + return [ + new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), + ]; + } + + protected function configureContainer(ContainerConfigurator $c): void + { + // PHP equivalent of config/packages/framework.yaml + $c->extension('framework', [ + 'secret' => 'S0ME_SECRET' + ]); + } + + protected function configureRoutes(RoutingConfigurator $routes): void + { + $routes->add('random_number', '/random/{limit}')->controller([$this, 'randomNumber']); + } + + public function randomNumber(int $limit): JsonResponse + { + return new JsonResponse([ + 'number' => random_int(0, $limit), + ]); + } } - public function randomNumber(int $limit): JsonResponse + $kernel = new Kernel('dev', true); + $request = Request::createFromGlobals(); + $response = $kernel->handle($request); + $response->send(); + $kernel->terminate($request, $response); + + .. code-block:: php-attributes + + // index.php + use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; + use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + use Symfony\Component\HttpFoundation\JsonResponse; + use Symfony\Component\HttpFoundation\Request; + use Symfony\Component\HttpKernel\Kernel as BaseKernel; + use Symfony\Component\Routing\Annotation\Route; + + require __DIR__.'/vendor/autoload.php'; + + class Kernel extends BaseKernel { - return new JsonResponse([ - 'number' => random_int(0, $limit), - ]); + use MicroKernelTrait; + + public function registerBundles(): array + { + return [ + new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), + ]; + } + + protected function configureContainer(ContainerConfigurator $c): void + { + // PHP equivalent of config/packages/framework.yaml + $c->extension('framework', [ + 'secret' => 'S0ME_SECRET' + ]); + } + + #[Route('/random/{limit}', name='random_number')] + public function randomNumber(int $limit): JsonResponse + { + return new JsonResponse([ + 'number' => random_int(0, $limit), + ]); + } } - } - $kernel = new Kernel('dev', true); - $request = Request::createFromGlobals(); - $response = $kernel->handle($request); - $response->send(); - $kernel->terminate($request, $response); + $kernel = new Kernel('dev', true); + $request = Request::createFromGlobals(); + $response = $kernel->handle($request); + $response->send(); + $kernel->terminate($request, $response); + +.. versionadded:: 6.1 + + The PHP attributes notation has been introduced in Symfony 6.1. That's it! To test it, start the :doc:`Symfony Local Web Server `: