Logic exception when trying to cache routes

68 views Asked by At

I'm trying to cache my routes using php artisan route:cache and it returns a logic exception because it thinks the route name is already used, which isn't the case. Here's the exception i get:

LogicException : Unable to prepare route [registreren] for serialization. Another route has already been assigned name [auth.register].

Here's my primary web.php routing file:

Route::name('auth.')->middleware(['basket'])->group(__DIR__ . '/web/auth.php');
Route::name('client.')->middleware(['basket'])->group(__DIR__ . '/web/client.php');
Route::prefix('admin')->name('admin.')->middleware(['admin'])->group(__DIR__ . '/web/admin.php');

Here's the excerpt of the auth.php routing file which throws the exception:

Route::controller(AuthRegisterController::class)->name('register')->middleware('guest')->group(function () {
    Route::get('/registreren', 'view');
    Route::post('/registreren', 'action');
});

I've searched through the routing files if i didn't accidently used the auth.register name twice but i didn't. However when i name the routes in the register group, it works:

Route::controller(AuthRegisterController::class)->name('register.')->middleware('guest')->group(function () {
    Route::get('/registreren', 'view')->name('get');
    Route::post('/registreren', 'action')->name('post');
});

Now i'm wondering, is this expected behaviour? The routing works perfectly without naming the get and post routes:

<a href="{{ route('auth.register') }}">
<form method="post" action="{{ route('auth.register') }}">

If this is expected behaviour, then the exception description is very misleading. I am hoping there's another solution to this problem because we've used the same approach for dozens of routes not to mention the hunderds of route calls in our views. Maybe it's a bug or i am doing something else wrong because it would require a huge refactor to name all the routes and route calls. Any thoughts would be very welcome, thanks.

1

There are 1 answers

0
aynber On BEST ANSWER

Named routes must be unique, even if they use different methods. You have the name on the group, so that name gets passed down to each of the routes in that group. You'll need to name each route separately.

Route::controller(AuthRegisterController::class)->middleware('guest')->group(function () {
    Route::get('/registreren', 'view')->name('register.view');
    Route::post('/registreren', 'action')->name('register.submit');
});