Skip to content

Route definitions incompatible with symfony/routing: 6.4.0 #52801

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
fritzmg opened this issue Nov 29, 2023 · 5 comments
Closed

Route definitions incompatible with symfony/routing: 6.4.0 #52801

fritzmg opened this issue Nov 29, 2023 · 5 comments

Comments

@fritzmg
Copy link
Contributor

fritzmg commented Nov 29, 2023

Symfony version(s) affected

6.4.0

Description

Currently I usually define my routes this way:

#Route(path: '/foobar', name: self::class)
class FoobarController
{
}

This way I can use the FQCN to generate the controller's URL:

$this->urlGenerator->generate(FoobarController::class)

This definition does not work in Symfony 6.4.0 anymore:

 [Symfony\Component\Config\Exception\LoaderLoadException]                                                                                                    
  Route alias "App\Controller\FoobarController" can not reference itself in src/Controller (which is being imported from "config/routes.yaml"). Make sure there is a loader supporting the "annotation" type.                                             
                                                                                                                                                              

Exception trace:
  at vendor\symfony\config\Loader\FileLoader.php:182
 Symfony\Component\Config\Loader\FileLoader->doImport() at vendor\symfony\config\Loader\FileLoader.php:98
 Symfony\Component\Config\Loader\FileLoader->import() at vendor\symfony\routing\Loader\YamlFileLoader.php:212
 Symfony\Component\Routing\Loader\YamlFileLoader->parseImport() at vendor\symfony\routing\Loader\YamlFileLoader.php:99

Changing the definition to

#Route(path: '/foobar')
class FoobarController
{
}

or another name for the route fixes the error, but I want to use the FQCN as the route name.

How to reproduce

Create a controller like this:

// src/Controller/FoobarController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/foobar', self::class)]
class FoobarController
{
    public function __invoke(): Response
    {
        return new Response('Hello World!');
    }
}

Then execute a cache:warmup.

Possible Solution

No response

Additional Context

No response

@HypeMC
Copy link
Contributor

HypeMC commented Nov 29, 2023

The issue was introduced in #50084. The simplest solution would be to remove self::class since this is now done implicitly:

-#Route(path: '/foobar', name: self::class)
+#Route(path: '/foobar')
class FoobarController
{
}

@fritzmg
Copy link
Contributor Author

fritzmg commented Nov 29, 2023

The simplest solution would be to remove self::class since this is now done implicitly:

That's not possible if your code needs to stay compatible with all currently supported versions of symfony/routing (e.g. symfony/routing: 5.4.*). Updating from Symfony 5.4 or 6.3 to 6.4 breaks these route definitions.

@fritzmg
Copy link
Contributor Author

fritzmg commented Nov 29, 2023

Also, is this feature working correctly? When executing

debug:router --show-aliases

I do not see any automatic FQCN aliases. But may be they are not supposed to.

@HypeMC
Copy link
Contributor

HypeMC commented Nov 29, 2023

Also, is this feature working correctly? When executing

debug:router --show-aliases

I do not see any automatic FQCN aliases.

Seems to be working in my case, but I've removed all the self::class names.

@fancyweb fancyweb self-assigned this Nov 29, 2023
@fancyweb
Copy link
Contributor

Thank you for your report, I'll have a look.

nicolas-grekas added a commit that referenced this issue Nov 29, 2023
…ncyweb)

This PR was merged into the 6.4 branch.

Discussion
----------

[Routing] Fix conflicting FQCN aliases with route name

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | #52801
| License       | MIT

When the route name === the FQCN alias we want to add, `addAlias()` throws. In this case, we can safely ignore the exception and move on.

Commits
-------

18494c8 [Routing] Fix conflicting FQCN aliases with route name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants