Route grouping in laravel

Share:
Grouping Routes in laravel

Sharing common requirements or attributes, such as middleware or namespaces with large number of routes needs you  to define those attributes on each individual route.Route groups allow you to share those attributes using Route::group method.You can pass these attribute as first argument of method.

Examples:-
Using Middleware in Route
Route::group(['middleware' => 'auth'], function () {
    Route::get('/foo', function ()    {
        // Uses Auth Middleware
    });

    Route::get('/foo/bar', function () {
        // Uses Auth Middleware
    });
});
Using Namespaces in Route

Route::group(['namespace' => 'Admin'], function()
{
    // Controllers Within The "App\Http\Controllers\Admin" Namespace

    Route::group(['namespace' => 'User'], function()
    {
        // Controllers Within The "App\Http\Controllers\Admin\User" Namespace
    });
});

Using Sub-Domain Routing

Route::group(['domain' => '{account}.xyz.com'], function () {
    Route::get('user/{id}', function ($account_name, $id) {
        //
    });
});
Route Prefixes

Route::group(['prefix' => 'admins'], function () {
    Route::get('users', function ()    {
        // Matches The "/admins/users" URL
    });
});

You can read more about routing from Routing in Laravel

Top Laravel Interview questions and answers for Fresher and Experienced 

No comments