-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[8.x] Add Route::missing() #36035
[8.x] Add Route::missing() #36035
Conversation
A couple ideas here Adam. One, would It might be smart to also accept a callback here, instead of providing the response straight up. That would allow you to lazily create the response, and potentially even get access to the request. Something like this: Route::get('/locations/{location:slug}', [LocationsController::class, 'show'])
->name('locations.view')
->missing(fn () => Redirect::route('locations.index', null, 301)); |
Missing, fallback, default? |
Thanks. Updated method to be called |
@hotmeteor did you test this with Route caching? Is your "missing" attribute persisted in that case? If so, how so when it is using a Closure? |
Thanks for pointing that out, I hadn't. I've updated this to handle caching, and tested it. As part of this modified the |
This reminds me of my "Internal Redirects" PR from a year ago. I still think it would be great if we could handle this through internal routing, rather than making a separate HTTP request. |
@browner12 My intent with this was that there specifically would be a separate HTTP request, because I want to report to Google that a page is no longer available. But if that could all be handled internally, then awesome. |
My original intent with the PR is that you could return a 404 status with a customized view that is still run through a controller. If you're sending a 404 a search engine should pick that up as a dead page, doesn't need to be a 3xx. |
You could just serve a 404 page with js that redirects the user. Then you get the correct status code and good UX. ;) |
Just so I'm understanding correctly, you are suggesting that as a way to solve OP's need with my internal routing solution, correct? |
It would be nice if this could work with a route group. I have a route group prefixed with a |
How about handling this method on resource controllers (rc)? |
@lucagrandicelli Look in |
Maybe is too late, but what about if you have a route with more than one implict-binded model ?` |
Coming late to this I have just started using missing() and finding it very useful and would like to echo the comment by @smarano that it would be even nicer if you could know which model was missing where there is more than one in the route. |
Would be great if one could directly return a view like for example... Route::get('/tags/{tag}', [TagController::class, 'show'])->missing(function (Request $request) {
return view('error');
}); In the above case it throws an error
|
Nevermind, i found the solution... Route::get('/tags/{tag}', [TagController::class, 'show'])->missing(function (Request $request) {
return response()->view('error');
}); |
This PR introduces a new
->missing()
method toRoute
.Here's the intention...
Let's say you have a route that's using implicit route-model binding (in this case, bound to a particular attribute):
Now, if that record doesn't exist, or has been removed, it'll throw a 404 if someone tries to access it. Currently to mitigate that you'd need to customize the controller method to check for it's existence and then handle it accordingly.
Instead, it would be great if there was an
->missing()
method that accepted a callback and handled the situation, ie:To do this we'd now be catching a
ModelNotFoundException
in theSubstituteBindings
middleware, but this seems acceptable since it'll only be doing this if it detects anmissing
fallback.Open to suggestions!