Skip to content

[FrameworkBundle] Add support for route attributes in kernel controller methods #17651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 91 additions & 37 deletions configuration/micro_kernel_trait.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
</setup/symfony_server>`:
Expand Down