Mastering Routes in Laravel: Best Practices .

Code Axion The Security Breach
4 min readFeb 4, 2023

Today i am gonna show you how to Organize Your Routes for the Large or Small Projects .

1st. Organize routes to separate files .

You might be having difficulty in organizing your routes in the web.php file, as all routes are clubbed into one single file and it can be a real hassle to find specific routes. For example, it may become challenging to differentiate between the admin routes, user routes, and role routes etc… , especially when you have a long list of routes in your web.php file and you may find yourself scrolling through numerous sections to find the specific route you’re looking for, which can be time-consuming and frustrating .

well i have an idea for you !

Suppose you have seller routes , buyer routes , admin routes ,user routes and some miscellaneous routes (notification, email) etc… now each of them contain more than 10 + routes , what you can do is create separate folders inside routes folder with the naming like this :

now as you have separated your routes into folders then it should work right ? No !

In order to make this work you have to define those routes in boot method in RouteServiceProvider.php just like this

public function boot()
{
$this->configureRateLimiting();

$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));

Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));

//-------------- NEW ROUTES ---------------------//
Route::middleware(['web','auth','admin'])
->namespace($this->namespace)
->group(base_path('routes/admin/admin.php'));

Route::middleware(['web','auth','seller'])
->namespace($this->namespace)
->group(base_path('routes/seller/seller.php'));

Route::middleware(['web','auth','buyer'])
->namespace($this->namespace)
->group(base_path('routes/buyer/buyer.php'));

Route::middleware(['web','auth'])
->namespace($this->namespace)
->group(base_path('routes/misc/misc.php'));
});
}

Here in middleware method you can give the middleware name of what middleware you wanna use and in the group path you will define your route file path .

This is the one way actually or you could just simply include the file just like this:

require __DIR__.'/admin/admin.php';

Well if you use the first idea you could get an idea how much Laravel framework is just capable of .

2nd. Using Route Controller method and Middleware method

Here you may see that are there several routes that are using the same controller and same Middlewares , this goes against DRY (Dont Repeat Yourself )Principle.

Bad:

Route::get('/admin/categories/index', [CategoryController::class, 'index'])->name('admin.category.index')->middleware(['auth','admin']);
Route::get('/admin/categories/create', [CategoryController::class, 'create'])->name('admin.category.create')->middleware(['auth','admin']);
Route::POST('/admin/categories/index', [CategoryController::class, 'store'])->name('admin.category.store')->middleware(['auth','admin']);
Route::get('/admin/categories/edit/{category}', [CategoryController::class, 'edit'])->name('admin.category.edit')->middleware(['auth','admin']);
Route::PUT('/admin/categories/update/{category}',[CategoryController::class,'update'])->name('admin.category.update')->middleware(['auth','admin']);
Route::DELETE('/admin/categories/delete/{category}', [CategoryController::class, 'delete'])->name('admin.category.delete')->middleware(['auth','admin']);

so what we can do is we can group the routes with the group() method and assign a single Controller and multiple middlewares using the controller() and middleware() methods, respectively, like this:

Good:

Route::controller(ProfileController::class)->middleware(['auth','admin'])->group(function() {

Route::get('/admin/categories/index', 'index')->name('admin.category.index');
Route::get('/admin/categories/create','create')->name('admin.category.create');
Route::POST('/admin/categories/index','store')->name('admin.category.store');
Route::get('/admin/categories/edit/{category}', 'edit')->name('admin.category.edit');
Route::PUT('/admin/categories/update/{category}','update')->name('admin.category.update');
Route::DELETE('/admin/categories/delete/{category}', 'delete')->name('admin.category.delete');

});

3rd. Group routes by names and prefixes .

Now if you carefully see we are using the same url for the crud which is “/admin/products/” and the same thing is happening with the route names , we are defining admin.products and /admin/products again and again just like the above example so what we can do is we can use prefix and name method into the group like this:

 Route::controller(CategoryController::class)
->middleware(['auth','admin'])
->prefix('admin/categories')
->namespace('Backend') //if you have one more folder inside Controllers you can specify namespaces too
->name('admin.category.')->group(function() {

Route::get('index', 'index')->name('index');
Route::get('create','create')->name('create');
Route::POST('index','store')->name('store');
Route::get('edit/{product}', 'edit')->name('edit');
Route::PUT('update/{category}','update')->name('update');
Route::DELETE('delete/{category}', 'delete')->name('delete');

});

this is also equivalent to this:

Route::group([

'middleware' => ['admin','auth'],
//if you have one more folder inside Controllers you can specify namespaces too
'namespace' => 'Backend',
'controller' => CategoryController::class,
'prefix' => 'admin/categories',
'as' => 'admin.category.',

],function() {

Route::get('index', 'index')->name('index');
Route::get('create','create')->name('create');
Route::POST('index','store')->name('store');
Route::get('edit/{category}', 'edit')->name('edit');
Route::PUT('update/{category}','update')->name('update');
Route::DELETE('delete/{category}', 'delete')->name('delete');
});

and is also equivalent to this too if you have separate file for routes (mentioned in first example) :

RouteServiceProvider.php

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

Route::middleware(['web','auth','admin']) //<------ your Middlewares array
->namespace($this->namespace . '\Backend') //<------ your Custom Namespace
->prefix('admin/categories') //<-------- URL prefixes
->name('admin.category.') //<----------- your routes names
->group(base_path('routes/admin.php')); //<-------- your routes file path

/routes/admin/admin.php


use Illuminate\Support\Facades\Route;

Route::controller(CategoryController::class)->group(function() {

Route::get('index', 'index')->name('index');
Route::get('create','create')->name('create');
Route::POST('index','store')->name('store');
Route::get('edit/{category}', 'edit')->name('edit');
Route::PUT('update/{category}','update')->name('update');
Route::DELETE('delete/{category}', 'delete')->name('delete');

});

You can see our routes looks much more sleeker now .

so if you want to organize your routes for Large/Small projects this is the best way .

Here are some key points of what we learned today :

1st. Organize Your Routes to separate folders

2nd. Use Middleware() and controller() method to share single controller and multiple middlewares to all the routes .

3rd: Use prefix() and name() method to share common url prefixes and route names to all the routes .

If you have read this far , I Really Appreciate Your Time . Thank you !

--

--

Code Axion The Security Breach

A Developer Who Enjoys Code Refactoring, Optimizing, and Improving The performance of Applications