In my Laravel app, I get a 404 error and I can't figure out why. The route correctly appears in artisan route:list
. I'm using route groupping and prefixes. Laravel debug output shows a different URI than expected. It seems that the books.archives
route redirects to the book.display
route. I just don't get it.
Here is an extract of my web.php :
// Books
Route::prefix('books')->name('books')->group(function() {
Route::get('/', [BooksController::class, 'list']);
Route::get('/create', [BooksController::class, 'create'])->name('.create');
Route::post('/', [BooksController::class, 'store'])->name('.store');
Route::get('/edit/{bookInfo}', [BooksController::class, 'edit'])->name('.edit');
Route::patch('/edit/{bookInfo}', [BooksController::class, 'update'])->name('.update');
Route::get('/{bookInfo}', [BooksController::class, 'display'])->name('.display');
Route::post('/delete/{id}', [BooksController::class, 'delete'])->name('.delete');
Route::prefix('archives')->name('.archives')->group(function() {
Route::get('/', [BooksController::class, 'archived']);
Route::get('/{bookInfo}', [BooksController::class, 'archive'])->name('.store');
Route::post('/delete/{bookInfo}', [BooksController::class, 'delete'])->name('.delete');
Route::post('/delete/all', [BooksController::class, 'deleteAll'])->name('.delete.all');
Route::get('/restore/{id}', [BooksController::class, 'restore'])->name('.restore');
});
});
The artisan route:list
output :
| | GET|HEAD | dashboard/books | books | App\Http\Controllers\BooksController@list | web |
| | | | | | App\Http\Middleware\Authenticate |
| | POST | dashboard/books | books.store | App\Http\Controllers\BooksController@store | web |
| | | | | | App\Http\Middleware\Authenticate |
| | GET|HEAD | dashboard/books/archives | books.archives | App\Http\Controllers\BooksController@archived | web |
| | | | | | App\Http\Middleware\Authenticate |
| | POST | dashboard/books/archives/delete/all | books.archives.delete.all | App\Http\Controllers\BooksController@deleteAll | web |
| | | | | | App\Http\Middleware\Authenticate |
| | POST | dashboard/books/archives/delete/{bookInfo} | books.archives.delete | App\Http\Controllers\BooksController@delete | web |
| | | | | | App\Http\Middleware\Authenticate |
| | GET|HEAD | dashboard/books/archives/restore/{id} | books.archives.restore | App\Http\Controllers\BooksController@restore | web |
| | | | | | App\Http\Middleware\Authenticate |
| | GET|HEAD | dashboard/books/archives/{bookInfo} | books.archives.store | App\Http\Controllers\BooksController@archive | web |
| | | | | | App\Http\Middleware\Authenticate |
| | GET|HEAD | dashboard/books/create | books.create | App\Http\Controllers\BooksController@create | web |
| | | | | | App\Http\Middleware\Authenticate |
| | POST | dashboard/books/delete/{id} | books.delete | App\Http\Controllers\BooksController@delete | web |
| | | | | | App\Http\Middleware\Authenticate |
| | GET|HEAD | dashboard/books/edit/{bookInfo} | books.edit | App\Http\Controllers\BooksController@edit | web |
| | | | | | App\Http\Middleware\Authenticate |
| | PATCH | dashboard/books/edit/{bookInfo} | books.update | App\Http\Controllers\BooksController@update | web |
| | | | | | App\Http\Middleware\Authenticate |
| | DELETE | dashboard/books/variations/delete/{book} | variations.delete | App\Http\Controllers\VariationsController@delete | web |
| | | | | | App\Http\Middleware\Authenticate |
| | GET|HEAD | dashboard/books/variations/edit/{book} | variations.edit | App\Http\Controllers\VariationsController@edit | web |
| | | | | | App\Http\Middleware\Authenticate |
| | PATCH | dashboard/books/variations/edit/{book} | variations.update | App\Http\Controllers\VariationsController@update | web |
| | | | | | App\Http\Middleware\Authenticate |
| | POST | dashboard/books/variations/{bookInfo}/add | variations.store | App\Http\Controllers\VariationsController@store | web |
| | | | | | App\Http\Middleware\Authenticate |
| | GET|HEAD | dashboard/books/variations/{bookInfo}/add | variations.create | App\Http\Controllers\VariationsController@create | web |
| | | | | | App\Http\Middleware\Authenticate |
| | GET|HEAD | dashboard/books/{bookInfo} | books.display | App\Http\Controllers\BooksController@display | web |
| | | | | | App\Http\Middleware\Authenticate |
The controller :
public function archived() {
$bookInfos = BookInfo::onlyTrashed()->get();
$archived = $bookInfos->count();
return view('books.archived', compact('bookInfos', 'archived'));
}
The debug output (from the page /dashboard/books/archives) :
What am I doing wrong ?
CodePudding user response:
As the path matches both "books.display" and "books.archives.archived" routes, maybe it is taking the first of them. Try putting the "archive" group of routes before the "book" routes:
// Books
Route::prefix('books')->name('books')->group(function() {
Route::prefix('archives')->name('.archives')->group(function() {
Route::get('/', [BooksController::class, 'archived']);
Route::get('/{bookInfo}', [BooksController::class, 'archive'])->name('.store');
Route::post('/delete/{bookInfo}', [BooksController::class, 'delete'])->name('.delete');
Route::post('/delete/all', [BooksController::class, 'deleteAll'])->name('.delete.all');
Route::get('/restore/{id}', [BooksController::class, 'restore'])->name('.restore');
});
Route::get('/', [BooksController::class, 'list']);
Route::get('/create', [BooksController::class, 'create'])->name('.create');
Route::post('/', [BooksController::class, 'store'])->name('.store');
Route::get('/edit/{bookInfo}', [BooksController::class, 'edit'])->name('.edit');
Route::patch('/edit/{bookInfo}', [BooksController::class, 'update'])->name('.update');
Route::get('/{bookInfo}', [BooksController::class, 'display'])->name('.display');
Route::post('/delete/{id}', [BooksController::class, 'delete'])->name('.delete');
});