Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ae445c

Browse files
dunglasfabpot
authored andcommitted
[FrameworkBundle] Add support for route attributes in kernel controller methods
1 parent 84d35a2 commit 0ae445c

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)