Laravel Routing: Top 10 tips for advance mastering Laravel Routing

Root User

Apr 23, 2020

In this article I will show some tips to improve your laravel routes. Before get started in the main topic I will explain the basics behind the laravel routes. By default laravel defines all routes in routes/web.php file.

<?php

Route::get('posts', '[email protected]');

Here, posts is the route that you access from http://domain.text/posts, the get is the http action verb, laravel suppports get, post and other custom http action verb as well as as put and delete. The PostController is the Controller Class in the file app\Http\Controllers\PostController.php and @index defines the index method of PostController class. So when you access http://domain.text/posts in the browser you would hit to the index method of PostController Class.

1. Use Route Resources

Suppose our application has a blogging features and following rourtes has defined in the routes files to perform CRUD operations on posts models. We can use Laravel Routes Resource instead of manullay defining them. For Example.

<?php

Route::get('posts', '[email protected]');
Route::show('posts/create', '[email protected]');
Route::post('posts', '[email protected]');
Route::show('posts/{post}', '[email protected]');
Route::get('posts/{post}/edit', '[email protected]');
Route::put('posts/{post}', '[email protected]');
Route::delete('posts/{post}', '[email protected]');

Instead of above seven routes, we can define the resource routes to perform the above same operations.

<?php

Route::resource('posts', 'PostController');

2. Stick with Laravel default Methods

Suppose we think of adding comments to our blog posts, we can define the custom routes methods the same controller. As follows:

<?php

Route::post('posts/{post}/add-comment', '[email protected]');

Rather than creating a custom methods in the PostController, Create new CommentController to handle Comment parts of blogging post.

<?php

Route::resource('comments', 'CommentController')->only(['store','update','delete']);

Here we define only the store,update, delete methods in the commentController and stick with the laravel default method.

3. Split Laravel Routes into multiple files

Suppose we have different types of users in our application for ex. Customers, Guest user etc. and Huge numbers of routes are associated with each types users. We can split our routes into different files. In my context I usually split into different files based on user role's action. To split into different files.

<?php
    
// app/Providers/RouteServiceProvider.php

namespace App\Providers;
    
class RouteServiceProvider extends ServiceProvider{
    
    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        .....
        $this->mapAdminRoutes();
    }
    
    
    /**
     * Defines routes for admin users
     *
     * @return void
     */
     protected function mapAdminRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/admin.php'));
    }
}    

Now you can create admin.php files in routes directory defines admin related routes in this file.

4. Group similar Prefix, Namespace and Middleware

Similar type of prefix, namespace and middleware can group together as follows. Let's continue above example, we want all the admin routes are prefixed with admin (http://domain.text/admin/admin-routes), we placed all the admin related controllers into the App\Http\Controllers\Admin namespace.

<?php
// routes/admin.php
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
    
});

To illustrate the above example, suppose we have following routes:

<?php
// routes/admin.php
Route::resource('admin/posts','Admin\PostController');
Route::resource('admin/comments','Admin\CommentController');
Route::resource('admin/author','Admin\AuthorController');

In the above example, we placed all the admin related operation into the Admin namespace i.e. ( App\Http\Controllers\Admin) and routes are prefixed with admin, This routes can further simplified as,

<?php
// routes/admin.php
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
    Route::resource('posts','PostController');
	Route::resource('comments','CommentController');
	Route::resource('author','AuthorController');
});
Related Articles