Skip to content

Commit

Permalink
feature #46009 [FrameworkBundle] Add support for first-class callable…
Browse files Browse the repository at this point in the history
… 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
-------

9c5be57 [FrameworkBundle] Add support for first-class callable route controller in MicroKernelTrait
  • Loading branch information
nicolas-grekas committed Apr 12, 2022
2 parents e1d46c9 + 9c5be57 commit 7751cd3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* Add `xliff` support in addition to `xlf` for `XliffFileDumper`
* Deprecate the `reset_on_message` config option. It can be set to `true` only and does nothing now
* Add `trust_x_sendfile_type_header` option
* Add support for first-class callable route controller in `MicroKernelTrait`

6.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}')) {
$route->setDefault('_controller', ['kernel', $r->name]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public function testFlexStyle()
$response = $kernel->handle($request);

$this->assertEquals('Have a great day!', $response->getContent());

$request = Request::create('/h');
$response = $kernel->handle($request);

$this->assertEquals('Have a great day!', $response->getContent());
}

public function testSecretLoadedFromExtension()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function __destruct()
protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->add('halloween', '/')->controller([$this, 'halloweenAction']);
$routes->add('halloween2', '/h')->controller($this->halloweenAction(...));
}

protected function configureContainer(ContainerConfigurator $c): void
Expand Down

0 comments on commit 7751cd3

Please sign in to comment.