Developer654079525's avatar

Organizing web routes

I have several routes, some are hard-coded some accepting parameters. I also have a big portion of admin routes, so I have to move the parametrized route all the way down, and would like to avoid that. The blueprint is:

Route::get('/', ...
Route::get('/about', ...
Route::get('/contact', ...

Route::get('/{slug}/{postslug?}', ...
    ->where('slug', '[A-Za-z0-9-_]+')
    ->where('postslug', '[A-Za-z0-9-_]+');

// and then a large number of hard-coded admin routes which are conflicting with the above parametrized one
Route::get('/admin-related1', ...
Route::get('/admin-related2', ...
// ...

How to organize things? The parametrized one is the one that gets the most traffic. Would it be sensible to keep the admin part in a separate file? I can move the parametrized one down, but would very much like to keep it above the admin section. I would also like to avoid route constraints, if possible.

0 likes
8 replies
Tray2's avatar
Tray2
Best Answer
Level 73

Since it's php you can separate them to your hearts content.

I created a helper for it.

function routesForModel($model)
{
    return require base_path("routes/Models/{$model}.php");
}

function authRoutes()
{
    return require base_path('routes/auth.php');
}

So I can just do it like this.

routesForModel('books');
routesForModel('games');
routesForModel('movies');
routesForModel('records');
routesForModel('profiles');
authRoutes();

And then in each "model route" file add the routes needed.

Route::get('/books', BooksIndexController::class)->name('books.index');
Route::get('/books/create', BooksCreateController::class)->name('books.create');
Route::get('/books/{bookShowView}', BooksShowController::class)->name('books.show');
Route::post('/books/store', BooksStoreController::class)->name('books.store');
Route::get('/books/edit/{bookShowView}', BooksEditController::class)->name('books.edit');
Route::put('/books/{book}', BooksUpdateController::class)->name('books.update');
Route::delete('/books/{book}', BooksDeleteController::class)->name('books.delete');
2 likes
Developer654079525's avatar

@Tray2 This is interesting. Is this helper a separate file or inline code in web.php? If separate, which folder should I put this helper in?

Glukinho's avatar

If you are concerned about convenience - you are free to organize routes as you like. Just try this, try that - and you will know what fits. Extracting your admin routes to a separate file sounds good.

If you are concerned about performance - php artisan route:cache caches all routes in effective way no matter how you organize them.

1 like
martinbean's avatar

@developer654079525 As others have said, you can organise routes how you wish.

For completely separate “sections” of applications, such as an admin panel, I extract those routes into their own routes file. I tend to name them something like routes/web.admin.php so it’s clear that it’s part of the “web” stack, but specifically the admin web routes.

1 like

Please or to participate in this conversation.