Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Routing] Route priority is lost when setting localized route prefix #52912

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
SystematicCZ opened this issue Dec 6, 2023 · 0 comments
Closed

Comments

@SystematicCZ
Copy link

SystematicCZ commented Dec 6, 2023

Symfony version(s) affected

6.4.1

Description

Route for controller B has priority set:

    #[Route(path:  '/b', name: 'b_method', priority: 9999)]
    public function bMethodOne(): Response

When the prefix in routes.yaml is commented out, the routing priority works as expected. The bin/console debug:router command outputs the following:

❯ bin/console debug:router
 ---------------- -------- -------- ------ -------------------------- 
  Name             Method   Scheme   Host   Path                      
 ---------------- -------- -------- ------ -------------------------- 
  b_method         ANY      ANY      ANY    /b                        
  _preview_error   ANY      ANY      ANY    /_error/{code}.{_format}  
  a_method         ANY      ANY      ANY    /a                        
 ---------------- -------- -------- ------ -------------------------- 

However, when the prefix is set, the routing priority seems to be lost. The bin/console debug:router command then shows a different order:

controllers:
    resource: '../src/**/*Controller.php'
    type: attribute
    prefix:
        cs: ~
        en: '/en'
❯ bin/console debug:router
 ---------------- -------- -------- ------ -------------------------- 
  Name             Method   Scheme   Host   Path                      
 ---------------- -------- -------- ------ -------------------------- 
  _preview_error   ANY      ANY      ANY    /_error/{code}.{_format}  
  a_method.cs      ANY      ANY      ANY    /a                        
  a_method.en      ANY      ANY      ANY    /en/a                     
  b_method.cs      ANY      ANY      ANY    /b                        
  b_method.en      ANY      ANY      ANY    /en/b                     
 ---------------- -------- -------- ------ -------------------------- 

How to reproduce

https://github.com/SystematicCZ/Symfony-Routing-bug

Possible Solution

I will try to submit PR soon

Additional Context

No response

nicolas-grekas added a commit that referenced this issue Jan 30, 2024
…ed prefix (pritasil)

This PR was merged into the 5.4 branch.

Discussion
----------

[Routing] Fixed priority getting lost when setting localized prefix

| Q             | A
| ------------- | ---
| Branch?       |  5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52912
| License       | MIT

When a route prefix is an array, the original route is removed from the collection and then re-added with a new name and path. During this process, the route's priority is lost as it's not preserved during the removal and re-addition.

Current state of `PrefixTrait.php`
```php
 $routes->remove($name);
 ...
  $routes->add($name.'.'.$locale, $localizedRoute);
 ```

To resolve this issue, I implemented a change where the priority of the route is stored before removal. This stored priority is then reassigned to the route when it's added back to the collection.

Commits
-------

ba41175 [Routing] Fixed priority getting lost when defining prefix array
nicolas-grekas added a commit that referenced this issue Nov 13, 2024
…uration (BeBlood)

This PR was merged into the 5.4 branch.

Discussion
----------

[Routing] Fix: lost priority when defining hosts in configuration

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

When host are configured in routing, the original route is removed from the collection and then re-added. During this process, the route's priority is lost as it's not preserved during the removal and re-addition.

Bug has similarities with #52912 (resolved for prefix)

Commits
-------

437e6ad [Routing] Fix: lost priority when defining hosts in configuration
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

3 participants