Skip to content

[FrameworkBundle] Add support for first-class callable route controller in MicroKernelTrait #46009

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 1 commit into from
Apr 12, 2022

Conversation

fancyweb
Copy link
Contributor

@fancyweb fancyweb commented Apr 12, 2022

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:

$routes->add('app_foo', '/foo')
    ->methods(['GET'])
    ->controller([$this, 'fooRoute']);

I'd like to switch them to:

$routes->add('app_foo', '/foo')
    ->methods(['GET'])
    ->controller($this->fooRoute(...));

Because I'd like to use first-class callable syntax everywhere in the project.

@@ -214,6 +214,8 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection

if (\is_array($controller) && [0, 1] === array_keys($controller) && $this === $controller[0]) {
$route->setDefault('_controller', ['kernel', $controller[1]]);
} elseif ($controller instanceof \Closure && $this === ($r = new \ReflectionFunction($controller))->getClosureThis() && !str_contains($r->name, '{closure}')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anonymous functions and outside closures will continue to fail.

@nicolas-grekas
Copy link
Member

Thank you @fancyweb.

@nicolas-grekas nicolas-grekas merged commit 7751cd3 into symfony:6.1 Apr 12, 2022
@fancyweb fancyweb deleted the fwb/kernel-fcc-route branch April 12, 2022 14:05
@fabpot fabpot mentioned this pull request Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants