8000 [Routing] Allow using invokable controller's FQCN (i.e. `::class`) to generate URLs (instead of route name) · Issue #49981 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Allow using invokable controller's FQCN (i.e. ::class) to generate URLs (instead of route name) #49981

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

Closed
ThomasLandauer opened this issue Apr 8, 2023 · 3 comments
Labels
Feature Help wanted Issues and PRs which are looking for volunteers to complete them. Routing

Comments

@ThomasLandauer
Copy link
Contributor
ThomasLandauer commented Apr 8, 2023

Description

With the advent of invokable controllers, it would be possible to move away from using names (i.e. strings) as route idenfifiers, and use the controller's FQCN instead:

#[Route(path: '/some')]
final class SomeController
{
    public function __invoke(): Response
    {
        // ...
    }
}

Then in any service:

$this->router->generate(SomeController::class);

Twig:

{{ path('App\\Controller\\SomeController') }}

Advantage: No more strings! :-)

I got the idea from https://tomasvotruba.com/blog/to-route-or-to-action-thats-the-question which says that this is possible in Laravel:

It allows using the controller directly instead of a middleman string:
+<form action="{{ action(\App\Controllers\ProcessPhpFormController::class) }}" method="post">

Example

No response

@derrabus
Copy link
Member
derrabus commented Apr 8, 2023

I like the idea. However, we currently generate a name for routes already, if the name property is omitted. Silently changing the strategy how that name is generated would be a BC break.

@ro0NL
Copy link
Contributor
ro0NL commented Apr 8, 2023

https://symfony.com/blog/new-in-symfony-5-4-route-aliasing#deprecating-routes 👀

@nicolas-grekas
Copy link
Member

Not sure I would deprecate the currently generated name, but why not create an alias indeed.
This would work only for invokable controllers.

@nicolas-grekas nicolas-grekas added the Help wanted Issues and PRs which are looking for volunteers to complete them. label Apr 19, 2023
@fabpot fabpot closed this as completed Jun 20, 2023
fabpot added a commit that referenced this issue Jun 20, 2023
…cable (fancyweb)

This PR was merged into the 6.4 branch.

Discussion
----------

[Routing] Add FQCN and FQCN::method aliases when applicable

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | #49981
| License       | MIT
| Doc PR        | -

See #49981, I think it's a great idea 😃

* We add an FQCN alias only if the target class has an `__invoke` method that adds a route AND if the target class added 1 route exactly.
* We add a FQCN::method alias for every method that defines only one route.

Commits
-------

9fa5bae [Routing] Add FQCN and FQCN::method aliases when applicable
symfony-splitter pushed a commit to symfony/routing that referenced this issue Jun 20, 2023 73D7
…cable (fancyweb)

This PR was merged into the 6.4 branch.

Discussion
----------

[Routing] Add FQCN and FQCN::method aliases when applicable

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | symfony/symfony#49981
| License       | MIT
| Doc PR        | -

See symfony/symfony#49981, I think it's a great idea 😃

* We add an FQCN alias only if the target class has an `__invoke` method that adds a route AND if the target class added 1 route exactly.
* We add a FQCN::method alias for every method that defines only one route.

Commits
-------

9fa5bae407 [Routing] Add FQCN and FQCN::method aliases when applicable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Help wanted Issues and PRs which are looking for volunteers to complete them. Routing
Projects
None yet
Development

No branches or pull requests

6 participants
0