Reputation: 91
I am using resource in laravel and i wanna a custom uri route for (show) action my route is:
Route::resource('/admin/users', 'UsersController')->except(['show']);
Route::get('/admin/users/{user}/show', 'UsersController@show')->name('users.show');
i checking a test url but not showing Error 404 in this Route for example :
http://127.0.0.1:8000/admin/users/test
show this error
The GET method is not supported for this route. Supported methods: PUT, PATCH, DELETE.
When I delete the code except(['show']) My code is working properly and shows a 404 error but my Route List Show Two Route for show action
| | GET|HEAD | backend/users/{user} | backend.users.show
| | GET|HEAD | backend/users/{user}/show | backend.users.show
Upvotes: 1
Views: 1112
Reputation: 91
I found the solution to this problem
Edit this file :
/app/Exceptions/Handler.php
Add this line
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
And
public function render($request, Throwable $exception)
{
if ($exception instanceof MethodNotAllowedHttpException)
{
abort(404);
}
return parent::render($request, $exception);
}
Upvotes: 0
Reputation: 1266
Try replacing except(['show'])
with
->only(['index', 'create', 'store', 'update', 'destroy'])
That will work fine in your case.
Upvotes: 0
Reputation:
This is because your route is NOT a 404 error as when you're using ::resource
when creating a route, it uses the same route as show()
as it does with the update()
and destroy()
methods but with a different request verb.
GET /photos/{photo}
PUT/PATCH. /photos/{photo}
DELETE /photos/{photo}
Read more about this in the documentation https://laravel.com/docs/7.x/controllers#resource-controllers
When you specify except(['show'])
you're removing the get route but the route still exists for the PUT/PATCH and DELETE methods.
Upvotes: 0
Reputation: 34668
Your both roure URI pattern are same, so you need to the define the route before the resource
route :
Route::get('/admin/users/{user}/show', 'UsersController@show')->name('users.show');
Route::resource('/admin/users', 'UsersController')->except(['show']);
Upvotes: 2