DEV Community

Cover image for Neat Routes in Laravel!
Reza Amini
Reza Amini

Posted on

Neat Routes in Laravel!

What is the Problem?

One of the programmer's concerns is refactoring.
Refactoring allows us to improve our code knowledge and code quality.

If have been familiar with the Laravel Routing system you know there are some files in routes/ directory which contain some routes for our project,api.php and web.php are more useful than other files.

When your project will grow up manage and edit these files are very harmful :(.

Imagine we have a blog project which has a dashboard and admin panel and a home page for everybody.

If we want to make these routes in web.php file after a while we will have an unreadable file and we didn't pay attention to the first principle of SOLID which tells us to "A class should have only one reason to change".

So what should We do?

How routes will be registerd?

If you check out the app/Providers/ directory you will see a file called RouteServiceProvider.php which contains our routes and it will register our route files.

In the boot method we have something like that:


$this->routes(function () {
    // It will register web.php route with 'web' middleware
    Route::middleware('web')
        ->group(base_path('routes/web.php'));


    // It will register api.php route with 'api' middleware and 'api' prefix
    Route::prefix('api')
        ->middleware('api')
        ->group(base_path('routes/api.php'));
});

Enter fullscreen mode Exit fullscreen mode

With the Route facade, we will be able to register our routes in different files.

So let's do that, let's register our routes.

1- Register Home Route:
We don't need to use middleware or a special prefix for this route, so register this in web.php:

Route::get('/', [HomeController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode

2- Register dashboard Routes:
First, we have to create a new file in the routes directory called dashboard.php which contains our dashboard routes:

Route::get('/', [DashboardController::class, 'index']);

Route::get('/edit', [DashboardController::class, 'showEdit']);
Route::post('/edit', [DashboardController::class, 'storeData']);
Enter fullscreen mode Exit fullscreen mode

Then we must register this file in our service provider with a /dashboard prefix and auth middleware.
Open the RouteServiceProvider.php and boot method, write some codes like this below:


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

   // It will register dasboard.php route with 'api' and 'auth' middleware and 'dashboard' prefix
    Route::middleware(['web', 'auth'])
        ->prefix('dashboard')
        ->group(base_path('routes/dasboard.php'));

    Route::prefix('api')
        ->middleware('api')
        ->group(base_path('routes/api.php'));
});

Enter fullscreen mode Exit fullscreen mode

Congrats! we registered our first route file :)

You can make more changes based on your project.

Point: This syntax belongs to Laravel 8 and in the previous versions we had some differences, but the scenario has not changed.

Discussion (0)