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 really helpful and time-saving. Hence, it is important for newcomers to know and extend their knowledge about routing.

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 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 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 attached 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 a number of 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}', '[email protected]');
            // ...
        });
    });
}

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}', '[email protected]');
        // ...
    });
});

Named Group Routes

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

Below is example of this process:

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

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

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

The above code generates route name for the users controller.

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

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share