Laravel returns a route not found exception whilst the route is defined

Viewed 143

I'm new to Laravel (I'm using Laravel 8 with sail on a Windows PC) and I've been trying to figure this out for quite a while now and can't find an explanation.

In my web.php file, I attempted to define the following route:

Route::get('/reservations', [ReservationController::class, 'create'])->name('reservation.create');

However, when I reference it on a blade with:

<a href="{{ route('reservation.create', $book->id) }}" class="btn btn-outline-primary">Reserve</a>

I get a route not found/defined exception.

But when I change the route to:

Route::get('/reservations', 'ReservationController@create')->name('reservation.create');

It works fine.

I'm using the first method for other routes and they work fine such as:

Route::get('/books/search', [BookController::class, 'search'])->name('books.search');

Can someone please tell me what I'm missing here?

(additionally) I have

use App\Http\Controllers\ReservationController;

in the web.php file,

namespace App\Http\Controllers;

in the controller and

protected $namespace = 'App\\Http\\Controllers';

in the RouteServiceProvider.php file and I've run all the artisan/composer commands to clear routes and caches etc I've found in other stack overflow questions.

(note that although I've resolved the issue using the 2nd method, the inconsistency in the web.php file is annoying to me)

Kind Regards

0 Answers
Related