How to Improve Routing In Laravel

Routing allows the user to route application requests to its appropriate controller. Laravel has an inbuilt API which is helpful and time-saving. Hence, newcomers need to know and extend their routing knowledge.

Users can learn about routing technique basics from here. Please go through the link to get more familiar with routing.

Here are a few tricks that can help you to use routing in Laravel:

Using Custom Namespace

Users can define a namespace for a group of routes using fluent routing API.

Route::namespace('Admin')->group(function () {
    // Controllers Within The "App\Http\Controllers\Admin" Namespace
});

Run the command given below to create a controller in App\Http\Controllers\Admin:

php artisan make:controller -r Admin/UsersController

After running the command, routes/web.php will look like this:

Route::namespace('Admin')
    ->prefix('admin')
    ->group(function () {
        Route::resource('users', 'UsersController');
    });

Debug Routes

To find or debug all defined routes, users can run php artisan route:list . Basically what this command does is that it helps to see the name of all routes and attach middleware to the route.

php artisan route:list
+--------+----------+----------+------+---------+--------------+
| Domain | Method   | URI      | Name | Action  | Middleware   |
+--------+----------+----------+------+---------+--------------+
|        | GET|HEAD | /        |      | Closure | web          |
|        | GET|HEAD | api/user |      | Closure | api,auth:api |
+--------+----------+----------+------+---------+--------------+

Route Macros

In Laravel, users can group several routes and send them via package and reusable code. For using macro, users can define a macro in the service provider. Below this section, we have given an example which of course is not the best example in the world but it will probably give you an idea about macro. 😀

Example: You have a shopping route that you send as a package to users. The user customizes the package:

// Inside a service provider boot()

public function boot()
{
    Route::macro('shopRoutes', function ($prefix) {
        Route::group([
            'prefix' => $prefix,
            'middleware' => ['shopping'],
        ], function () {
            Route::get('products/{product}', 'ProductsController@show');
            // ...
        });
    });
}

which the user can call in the application inside routes/web.php.

collect(config('languages'))->each(function ($language) {
    Route::shopRoutes($language);
});

There is another alternative method for this process:

Route::macro('shopRoutes', function ($languages) {
    Route::group([
        'prefix' => '/{language}',
        'middleware' => ['shopping'],
        'where' => ['language' => implode('|', $languages)],
    ], function () {
        Route::get('products/{product}', 'ProductsController@show');
        // ...
    });
});

Named Group Routes

Naming routes is a common yet reliable way to improve routing. This helps in referencing the name of the route easily.

Below is an example of this process:

{{ route('admin.users.show', ['user' => $user]) }}
{{-- /admin/users/2 --}}

Users can also prefix the name of a route while defining a group of routes. Like:

Route::namespace('Admin')
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {
        Route::resource('users', 'UsersController');
    });

The above code generates a route name for the user’s controller.

Get further knowledge about routing from routing documentation by clicking here.

Asmit Nepali, a Full Stack Developer, holds a Software Engineering degree from Gandaki College of Engineering and Science. Proficient in PHP Laravel, Vue.js, MySQL, Tailwind, Figma, and Git, he possesses a robust technical skill set.

Leave a Comment