Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

Problem with routes

Hey!

I have the following structure of my routes/web.php

viz1

and each group has routes like index, add, edit, delete etc...

// ...

// News
Route::name('news.')->prefix('news')->group(function() {
     Route::name('index')->get('/', [AdminController::class, 'newsIndex']);
     Route::name('add')->get('/', [AdminController::class, 'newsAdd']);
     Route::name('edit')->get('/', [AdminController::class, 'newsEdit']);
     // ....
});

// ...

But unfortunately I faced some problems. Last 2 group creations which are actually the sources. and ads. groups, some of the routes inside these groups are not correctly handled by Laravel because their route names are actually bugging. Example: The current bug I'm facing is inside the News Source Group:

// News Sources
Route::name('sources.')->prefix('news/sources')->middleware('onlyAdministrators')->group(function() {

    Route::name('index')->get('/', [AdminController::class, 'newsSourcesIndex']);
    Route::name('add')->get('/add', [AdminController::class, 'newsSourcesAdd']);

});

as you can see I'm having 2 routes which must correspond to admin.sources.index and admin.sources.add but Laravel is returning me an error Route [admin.sources.add] not defined. when trying to put a link to this route inside a <a href="{{ route('admin.sources.add') }}"> tag. I tried clearing all the possible caches through arisan commands.

php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan view:cache
php artisan event:clear // which actually has nothing to do with this problem
php artisan optimize:clear
php artisan optimize
php artisan cache:clear

but inside the route:list the admin.sources.add route is still with a route name of admin.sources.. What might be the problem? I'm really stuck.

0 likes
17 replies
Sinnbeck's avatar

To make sure a route isnt captured by a previous, try moving the group that isnt working to the top of the admin group

lat4732's avatar
Level 12

@Sinnbeck Moving the whole News Sources group at the top of the admin group changed their route names in route:list to admin.sources. for both. Previously only one was admin.sources. and the other one was something like admin.sources.generated-1258219060873128632

Sinnbeck's avatar

Generated?! I have never seen that in a name before

lat4732's avatar
Level 12

@Sinnbeck I swear the add route's name was something like admin.sources.generated-SOME-NUMBERS. Now both are with admin.sources. name (literally with the dot at the end). I'm checking them in route:list

lat4732's avatar
Level 12

@Sinnbeck I forgot to say that the application is in production (APP_ENV=production) and everything is cached. (if matters at all)

lat4732's avatar
Level 12

@Sinnbeck I dunno because I'm working directly through SSH access with the files. But I didn't had any problems while working locally. But back then the routes were like 15 in total so...

lat4732's avatar
Level 12

@Sinnbeck Another thing is that the problem occurred like 2 hours ago when I tried to add a new route group (ads.). Everything worked fine untill I added the add route. And I know this because I had the same problem with Route [...] not defined. and I just decided to use url('admin/ads/add') instead of route('admin.ads.add'). Do you see anything strange in this route and can it cause these issues? Any suggestions to debug this problem somehow? I'm really stuck.

Route::name('sources.')->prefix('news/sources')->middleware('onlyAdministrators')->group(function() {

Route::name('index')->get('/', [AdminController::class, 'newsSourcesIndex']);
Route::name('add')->get('/add', [AdminController::class, 'newsSourcesAdd']); <-- THIS

});
lat4732's avatar
Level 12

@Tray2 I'll keep writing my routes manually for now as I want to have control over everything. How to do you apply middleware(s) to a single route of a Resource Route?

Muhammad_Shoaib_Ali's avatar
Level 1

Use a that way

Route::get('/add', [AdminController::class, 'newsSourcesAdd'])->name('add');

1 like

Please or to participate in this conversation.