[9.x] Fixed RoueGroup::merge
to format merged prefixes correctly
#44011
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes issue described in #43997
Problem this PR fixes
If route has parent RouteGroup with its prefix not set. FormatPrefix adds slash to either start or end of the actual prefix depending on the situtation.
This problem affects the whole
web.php
file, because the routes inside are loaded using group with empty $attributes array (so its prefix is empty).Examples of the problem
Appending prefix
Expected behavior:
When user visits */dashboard/settings the
Route->current()->getPrefix()
should returndashboard
Actual behavior:
The route prefix is returned with slash at the start ->
/dashboard
Prepending prefix
Expected behavior:
When user visits */dashboard/settings the
Route->current()->getPrefix()
should returndashboard
Actual behavior:
The route prefix is returned with slash at the end ->
dashboard/
Quick side note: I don't even know if this syntax is allowed. I didn't found any mentions about prepending route prefixes in the routing documentation, yet there is a test for this exact behavior.
framework/tests/Routing/RoutingRouteTest.php
Lines 1159 to 1170 in 7fa4beb
Here the
bar
prefix is before the groupfoo
prefix. Event tho its defined inside the group.Source of the problem
If i understood the problem correctly i think the issue is in the
RouteGroup::formatPrefix
method.framework/src/Illuminate/Routing/RouteGroup.php
Lines 64 to 73 in 7fa4beb
Here the
$old
variable is set to empty string, when the parent RouteGroup prefix is empty / not set.And now when the old prefix is empty and new prefix is set.
Appending prefix
This is what happens in the code on line 69 ->
"" . "/" . "newPrefix"
There is slash added to the start of the new prefix.
Prepending prefix
This is what happens in the code on line 71 ->
"newPrefix" . "/" . ""
There is slash added to the end of the new prefix.
My solution
I managed to fix this issue by wrapping the return from
formatPrefix
in trim.Reference:
framework/src/Illuminate/Routing/RouteGroup.php
Lines 64 to 73 in 7fa4beb
My fix:
This should guarantee the prefix is always returned without slash at the start or end as expected.
I also tried running all the tests in the Routing folder after this fix and all of them have passed.
I hope my understanding of the problem is correct and the explanation was clear enough. :)