Laravel 8 Route Groups Example

Laravel 8 Route Groups Example

1. Basic Route Group

You can group routes without any additional attributes. For example, you can group routes under a common URL prefix:

// routes/web.php Route::group([], function () { Route::get('/home', function () { return 'Home Page'; }); Route::get('/about', function () { return 'About Page'; }); });

In this case, both /home and /about routes will work as normal without any changes to their URLs.

2. Applying a URL Prefix

One common use of route groups is to apply a URL prefix to all routes in the group. This is useful when you want to group routes under a common prefix, such as /admin or /user.

// routes/web.php Route::group(['prefix' => 'admin'], function () { Route::get('/dashboard', function () { return 'Admin Dashboard'; }); Route::get('/settings', function () { return 'Admin Settings'; }); });

This will result in the following routes:

  • /admin/dashboard

  • /admin/settings

3. Applying Middleware to a Route Group

You can apply middleware to a group of routes, meaning all the routes in the group will use the same middleware. For example, applying the auth middleware to ensure the user is authenticated before accessing the routes:

// routes/web.php Route::group(['middleware' => 'auth'], function () { Route::get('/profile', function () { return 'User Profile'; }); Route::get('/settings', function () { return 'User Settings'; }); });

Here, both /profile and /settings routes will require the user to be authenticated before they can access the pages.

4. Applying a Namespace to a Group

If you want to group routes under a common namespace for controllers, you can use the namespace attribute. This is useful for organizing controllers into different folders, such as Admin or User controllers.

// routes/web.php Route::group(['namespace' => 'Admin'], function () { Route::get('/dashboard', 'DashboardController@index'); Route::get('/settings', 'SettingsController@index'); });

In this case, Laravel will look for the DashboardController and SettingsController in the App\Http\Controllers\Admin namespace.

5. Applying Multiple Attributes to a Group

You can combine multiple attributes in a route group. For example, you can apply both a prefix, middleware, and namespace at the same time.

// routes/web.php Route::group(['prefix' => 'admin', 'middleware' => 'auth', 'namespace' => 'Admin'], function () { Route::get('/dashboard', 'DashboardController@index'); Route::get('/settings', 'SettingsController@index'); });

This group will:

  • Prefix the routes with /admin

  • Require the user to be authenticated using the auth middleware

  • Use the App\Http\Controllers\Admin namespace for the controllers

This will result in the following routes:

  • /admin/dashboard (with auth middleware)

  • /admin/settings (with auth middleware)

6. Named Routes with Groups

You can also define named routes within a group. Named routes are useful for generating URLs or redirects in your application.

// routes/web.php Route::group(['prefix' => 'admin'], function () { Route::get('/dashboard', function () { return 'Admin Dashboard'; })->name('admin.dashboard'); Route::get('/settings', function () { return 'Admin Settings'; })->name('admin.settings'); });

Now you can generate URLs for these routes using the route name:

$url = route('admin.dashboard'); // /admin/dashboard

7. Resource Routes in a Group

You can also define resource routes within a group, which is particularly useful for CRUD operations.

// routes/web.php Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () { Route::resource('posts', 'Admin\PostController'); });

This will create the following routes for the PostController:

  • /admin/posts (GET) → index

  • /admin/posts/create (GET) → create

  • /admin/posts (POST) → store

  • /admin/posts/{post} (GET) → show

  • /admin/posts/{post}/edit (GET) → edit

  • /admin/posts/{post} (PUT/PATCH) → update

  • /admin/posts/{post} (DELETE) → destroy

Conclusion

Route groups in Laravel are a powerful feature to organize and manage your routes more effectively. By grouping routes, you can apply common attributes like prefixes, middleware, and namespaces, which helps keep your code clean and maintainable.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

1 Comments

CAN FEEDBACK
  1. q tech
    q tech
    Quleiss work as a partner to provide assistance to the businesses at a most affordable price in the competitive market. We are one of the leading Web development company in pune as well as the mobile apps development company in pune. We work as an end to end business solution provider. Offering tech and digital marketing solutions that build brand recognization and offer real solutions to business challenges. We help our client’s to achieve their most important goals and make long lasting improvements to their business.
close