8000 feature #46115 [FrameworkBundle] Add support for route attributes in … · symfony/symfony@e740a11 · GitHub
[go: up one dir, main page]

Skip to content

Commit e740a11

Browse files
committed
feature #46115 [FrameworkBundle] Add support for route attributes in kernel controller methods (dunglas)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- [FrameworkBundle] Add support for route attributes in kernel controller methods | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | todo Simplify creating single file projects using route attributes: ```php <?php require __DIR__.'/vendor/autoload.php'; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Kernel as BaseKernel; use Symfony\Component\Routing\Annotation\Route; class Kernel extends BaseKernel { use MicroKernelTrait; #[Route('/')] public function homepage() { return new Response('Hello, world'); } } $app = new Kernel($_SERVER['APP_ENV'] ?? 'prod', $_SERVER['APP_DEBUG'] ?? false); if (\PHP_SAPI === 'cli') { $application = new Application($app); exit($application->run()); } $request = Request::createFromGlobals(); $response = $app->handle($request); $response->send(); $app->terminate($request, $response); ``` This will also allow removing these lines from the FrameworkBundle recipe: https://github.com/symfony/recipes/blob/master/symfony/routing/6.0/config/routes.yaml#L5-L7 Commits ------- 0ae445c [FrameworkBundle] Add support for route attributes in kernel controller methods
2 parents 84d35a2 + 0ae445c commit e740a11

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Add `trust_x_sendfile_type_header` option
1414
* Add support for first-class callable route controller in `MicroKernelTrait`
1515
* Add tag `routing.condition_service` to autoconfigure routing condition services
16+
* Automatically register kernel methods marked with the `Symfony\Component\Routing\Annotation\Route` attribute or annotation as controllers in `MicroKernelTrait`
1617

1718
6.0
1819
---

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ private function configureRoutes(RoutingConfigurator $routes): void
8282
} else {
8383
$routes->import($configDir.'/{routes}.php');
8484
}
85+
86+
if (false !== ($fileName = (new \ReflectionObject($this))->getFileName())) {
87+
$routes->import($fileName, 'annotation');
88+
}
8589
}
8690

8791
/**

src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public function testFlexStyle()
9494
$response = $kernel->handle($request);
9595

9696
$this->assertEquals('Have a great day!', $response->getContent());
97+
98+
$request = Request::create('/easter');
99+
$response = $kernel->handle($request);
100+
101+
$this->assertSame('easter', $response->getContent());
97102
}
98103

99104
public function testSecretLoadedFromExtension()

src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/flex-style/src/FlexStyleMicroKernel.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
use Symfony\Component\Filesystem\Filesystem;
1919
use Symfony\Component\HttpFoundation\Response;
2020
use Symfony\Component\HttpKernel\Kernel;
21+
use Symfony\Component\Routing\Annotation\Route;
2122
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
2223

2324
class FlexStyleMicroKernel extends Kernel
2425
{
25-
use MicroKernelTrait;
26+
use MicroKernelTrait {
27+
configureRoutes as traitConfigureRoutes;
28+
}
2629

2730
private $cacheDir;
2831

@@ -31,6 +34,12 @@ public function halloweenAction(\stdClass $o)
3134
return new Response($o->halloween);
3235
}
3336

37+
#[Route('/easter')]
38+
public function easterAction()
39+
{
40+
return new Response('easter');
41+
}
42+
3443
public function createHalloween(LoggerInterface $logger, string $halloween)
3544
{
3645
$o = new \stdClass();
@@ -73,6 +82,8 @@ public function __destruct()
7382

7483
protected function configureRoutes(RoutingConfigurator $routes): void
7584
{
85+
$this->traitConfigureRoutes($routes);
86+
7687
$routes->add('halloween', '/')->controller([$this, 'halloweenAction']);
7788
$routes->add('halloween2', '/h')->controller($this->halloweenAction(...));
7889
}

0 commit comments

Comments
 (0)
0