-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Different getPrefix() behavior for cached and non-cached routes #50239
Comments
@kudashevs does the solution for #43932 work if you apply the patch locally? We could maybe try to revive it with proper tests. |
Actually, we also tried this at #44011 but that trims both sides and broke things for some people. So not sure if this one is solvable at all. Although I agree the behaviour should be the same in both cached and none-cached routes. From a first glance I think the cached route value is the correct one (without the prefixed |
@driesvints the patch adds a forward slash to both (cached and non-cached) routes |
From the first glance I agree. But, it’s not so obvious. In the documentation all the route examples with paths have a forward slash. On the other hand, the slash is optional because it is going to be removed during the parsing process. In the community people use both notations (with forward slash and without). The only prefix example goes without a forward slash. However, due to the logic of the aforementioned route examples it should be expanded to something with a forward slash (which is going to be removed during the parsing process). But the So, I think, to move forward we need someone who is in charge of making a final decision on this. I mean, the final decision on the expected behavior (should a route go with a forward slash or without a slash; in which cases a slash should be added). And, I would expect the behavior to be the same for cached and non-cached routes. P.S. the things are even more complicated because the |
What's that? |
Just in case, for those, who use the
(app()->routesAreCached())
? $route->getPrefix() === '{locale}'
: $route->getPrefix() === '/{locale}';
ltrim($route->getPrefix(), '/') === '{locale}'; I would prefer the second one. However, you might want to keep the reason of using the different comparisons. |
It's been almost three months without any progress. |
No. I'm sorry but I just haven't gotten to this yet. Honestly I do not know what the correct path forward is. I think cached routes should mimic what uncached routes do personally. But I don't have the time to work on a solution. If you could work on a PR and send it in then we can go from there. Thanks |
I also don't know how to approach this issue. What makes the issue even worse is that it seems that this is the default behavior for all the versions (at least I checked 9, 10, 11).
As mentioned earlier, without having the requirements (or at least an explanatory post with a thorough explanation on how the cached and non-cahced routes should behave) approved by Taylor (or someone else who is in charge of making the final decision on the system's behavior) it is going to be a sort of shot in the dark. |
I also think cached routes should exactly mimic the behavior of uncached routes. In other words, if cached routes are currently different than uncached, they should be updated to match the uncached behavior. |
Hello there, I played around with the issue today, and I didn't find any way to write tests for this issue. What do I mean. To check whether the cached routes behave in the correct way, they should be cached. The usual way to cache them is to use the artisan command. So, I used the testRouteGrouping method as a basis, but it turned out that you cannot just call an artisan command without a bootstrapped app (that makes sense). So, I thought to go in a different direction and use I thought, that mocking a cache path might be an option here. I mocked the $router = $this->getRouter();
$router->group(['prefix' => 'foo'], function () use ($router) {
$router->get('bar', function () {
return 'hello';
});
});
$routes = $router->getRoutes();
$routes = $routes->getRoutes(); doesn't work in this case. The command just doesn't get any routes. Have I missed something? Do you have any ideas or suggestions on how the tests could be implemented? Or, do you have any code examples of the tests that work with cached routes (I didn't find any)? Is there a way to cache the routes without the artisan command and without duplicating its functionality? Any idea or advice might be helpful in this case. |
@crynobone would you maybe know how we can test cached routes in framework using test bench? |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
laravel#50239 Fix:The getPrefix() method of the Illuminate\Routing\Route returns different values for non-cached and cached routes: /{locale} - the return value of the getPrefix() method for a non-cached route {locale} - the return value of the getPrefix() method for a cached route
Hi @kudashevs. Since it seems very hard to solve this one I'm going to close this issue now. Unfortunately we cannot bring these two behaviours closer at this time. |
Laravel Version
10.45.1
PHP Version
8.1.27
Database Driver & Version
No response
Description
The
getPrefix()
method of theIlluminate\Routing\Route
returns different values for non-cached and cached routes:/{locale}
- the return value of the getPrefix() method for a non-cached route{locale}
- the return value of the getPrefix() method for a cached routeThis discrepancy in the results for non-cached/cached routes seems a sort of bug to me.
This issue might be related with #43882 and #43997.
Steps To Reproduce
After installing a new Laravel project, just add a route group to the
web
routes (the/routes/web.php
file). For example:Then, use the
tinker
tool to get the registered routes and check thegetPrefix()
return values.Let's check the non-cacned routes:
The result is going to be:
Then, we can cache the routes with
artisan route:cache
command and usetinker
again.The result is going to be:
The text was updated successfully, but these errors were encountered: