Resource Controller registration causes non-resource routes to be ignored #58127
Replies: 11 comments
-
|
Just to confirm, you're saying this has been an issue since at least 12.36.1, right? |
Beta Was this translation helpful? Give feedback.
-
|
You are declaring two similar routes. GET Declare |
Beta Was this translation helpful? Give feedback.
-
Yes. I originally discovered this at work, in a 12.36.1 application, but since it also uses Vue, Inertia, and many other packages, I created a new application as bare as possible to confirm it, in case it was a controller override, or some package interfering with the route definitions. I was a little rushed so I forgot to mention that the project I discovered this in also contains numerous instances of this pattern where the issue does NOT occur, and I don't currently have any idea why, which is why this confused me so much. |
Beta Was this translation helpful? Give feedback.
-
I do know that switching the order fixes the issue, as I wrote above. |
Beta Was this translation helpful? Give feedback.
-
Not sure if there is a way to “fix it,” since it's not broken. Ideally, you should use Also, this has been an “issue” at least since Laravel 6, which is the first version I used. |
Beta Was this translation helpful? Give feedback.
-
Perhaps you are using the |
Beta Was this translation helpful? Give feedback.
-
|
I also agree that this is not technically a problem, however I also think it can be a source of a lot of frustration (I've seen at least one senior dev debugging a "missing" route and not noticing that this is what happened) I think we could be more conceptually consistent if we throw an error when we come upon an unreachable route, the same way we throw an error if there are two different routes with the same name. |
Beta Was this translation helpful? Give feedback.
-
|
Interesting how (overhead) features are complicating devs lifes when they were intended to ease them... Developer convenience... |
Beta Was this translation helpful? Give feedback.
-
I do understand how routing works generally, but if I had to read the entire source of the framework before defining routes, I'd get nowhere. Clearly what I missed is that the router doesn't simply skip over conflicts but instead creates a second definition with overlapping match criteria. It'd be nice if this situation had a clear exception message, and/or a better way of avoiding it than having to remember which order to place the route definitions in, rather than a developer (me) having to spend an hour debugging to figure out why a route that is clearly defined returns a 404. |
Beta Was this translation helpful? Give feedback.
-
|
@bluesheep100 Sadly all Laravel is built like this. You have to know what is hidden under the carpet in order to not get nasty surprises. This is why I avoid using features from it when I can do them my self in an easier and more explicit way. |
Beta Was this translation helpful? Give feedback.
-
|
I personally think most of the value in Laravel and similar frameworks is lowering the barrier to entry by allowing devs to avoid reinventing the wheel and avoid running into common issues. I have never contributed to Laravel, but I would like to get started. If any decision is reached on a solution I would like to submit a PR for it, if possible. Here are the two possible solutions I have in mind (I am open to discuss and/or implement others) A. Simply throwing an error about conflicts on
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
12.38.1
PHP Version
8.3.27
Database Driver & Version
SQLite 3.37.2
Description
Routes using a resources prefix (e.g. /users for the User model) placed after a Route::resource() call seem to be ignored, but still shown in the list when running
php artisan route:list.Steps To Reproduce
Discovered on v12.36.1, verified on v12.38.1
Tested on a brand new starter kit free application:
laravel new routetesterTest case setup:
php artisan make:controller ExampleController -rreturn to_route('welcome');to theindex()methodThen add this method to the controller:
And the following route definitions to
routes/web.php:Run
php artisan serveand visitlocalhost:8000/examples, which should display the welcome page.Visiting
localhost:8000/examples/testhowever, gives a blank white page, with 200 OK as the status code. If the order of the route definitions are switched, the welcome page is displayed.If using a DELETE request instead of GET, this issue seems to end in a
404 Not Found.Beta Was this translation helpful? Give feedback.
All reactions