This is my route
Route::resource('/posts', PostController::class)->middleware('auth');
Route::get('/posts/drafts', [PostController::class, 'draft'])->middleware('auth');
Route::put('/posts/{id}/reject', [PostController::class, 'reject'])->middleware('auth');
Route::put('/posts/{id}/publish', [PostController::class, 'publish'])->middleware('auth');
when i access /posts/drafts
it's always not found, but when i disable the route resource, it can.
it's doesn't happen with /posts/{id}/reject
and /posts/{id}/publish
.
what is the solution ?
i tried to search in google but i confused.
i expect it gonna work and i understand why this happen
Edit : SOLVED
CodePudding user response:
For anyone stuck on this problem,
Try moving your custom/additional routes above the resource routes:
Route::get('/posts/drafts', [PostController::class, 'draft'])->middleware('auth');
Route::put('/posts/{id}/reject', [PostController::class, 'reject'])->middleware('auth');
Route::put('/posts/{id}/publish', [PostController::class, 'publish'])->middleware('auth');
Route::resource('/posts', PostController::class)->middleware('auth');
This could also be wrapped as(Wrap the routes in a group):
Route::group(['middleware'=>'auth'], function(){
Route::get('/posts/drafts', [PostController::class, 'draft']);
Route::put('/posts/{id}/reject', [PostController::class, 'reject']);
Route::put('/posts/{id}/publish', [PostController::class, 'publish']);
Route::resource('/posts', PostController::class);
});
More Info here: https://laravel.com/docs/9.x/controllers#restful-supplementing-resource-controllers