Skip to content

Commit 85ebd85

Browse files
feature #46009 [FrameworkBundle] Add support for first-class callable route controller in MicroKernelTrait (fancyweb)
This PR was merged into the 6.1 branch. Discussion ---------- [FrameworkBundle] Add support for first-class callable route controller in MicroKernelTrait | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - I have some routes defined in `Kernel.php` with the following syntax: ```php $routes->add('app_foo', '/foo') ->methods(['GET']) ->controller([$this, 'fooRoute']); ``` I'd like to switch them to: ```php $routes->add('app_foo', '/foo') ->methods(['GET']) ->controller($this->fooRoute(...)); ``` Because I'd like to use first-class callable syntax everywhere in the project. Commits ------- 9c5be576fb [FrameworkBundle] Add support for first-class callable route controller in MicroKernelTrait
2 parents 81e3e6e + f469976 commit 85ebd85

File tree

4 files changed

+9
-0
lines changed

4 files changed

+9
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Add `xliff` support in addition to `xlf` for `XliffFileDumper`
1212
* Deprecate the `reset_on_message` config option. It can be set to `true` only and does nothing now
1313
* Add `trust_x_sendfile_type_header` option
14+
* Add support for first-class callable route controller in `MicroKernelTrait`
1415

1516
6.0
1617
---

Kernel/MicroKernelTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
214214

215215
if (\is_array($controller) && [0, 1] === array_keys($controller) && $this === $controller[0]) {
216216
$route->setDefault('_controller', ['kernel', $controller[1]]);
217+
} elseif ($controller instanceof \Closure && $this === ($r = new \ReflectionFunction($controller))->getClosureThis() && !str_contains($r->name, '{closure}')) {
218+
$route->setDefault('_controller', ['kernel', $r->name]);
217219
}
218220
}
219221

Tests/Kernel/MicroKernelTraitTest.php

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

9191
$this->assertEquals('Have a great day!', $response->getContent());
92+
93+
$request = Request::create('/h');
94+
$response = $kernel->handle($request);
95+
96+
$this->assertEquals('Have a great day!', $response->getContent());
9297
}
9398

9499
public function testSecretLoadedFromExtension()

Tests/Kernel/flex-style/src/FlexStyleMicroKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function __destruct()
7474
protected function configureRoutes(RoutingConfigurator $routes): void
7575
{
7676
$routes->add('halloween', '/')->controller([$this, 'halloweenAction']);
77+
$routes->add('halloween2', '/h')->controller($this->halloweenAction(...));
7778
}
7879

7980
protected function configureContainer(ContainerConfigurator $c): void

0 commit comments

Comments
 (0)