Laravel 8 Route Groups Example

Laravel 8 Route Groups Example

 

Laravel 8 Route Groups Example



Laravel 8 route groups allow you to group all the routes related to your modules. Routes Groups are helpful in the situation when you want to apply one attribute to all the routes.

Laravel 8 Route Groups

Route group takes an array that can take attributes and callback functions. The route group allows you to share attributes such as middleware, prefixed, or namespaces without defining these attributes on each route.

These shared attributes can be passed in the array format as the first argument to the Route::group() function.

For example, if you want to apply a namespace to different routes, then you can do it by creating a route group and then apply the namespace to that group.

Syntax

Route::group( [ ] , callback);

Parameters

The group() function takes an array passed to the group method as a first parameter. The second parameter is the callback function.

Define Route Groups in Laravel 8

Before defining route groups, let’s create a namespace for our project.

Step 1: Define namespace.

Laravel namespaces are defined as a class of elements in which each element has a unique name to the associated class.

To create a custom namespace in Laravel, create a separate controller with forward-slash(/) using the following command.

php artisan make:controller Admin/UserController --resource --model=User

The UserController will be created inside the Admin directory inside the app >> Http >> Controllers folder.

Add the following code inside the UserController’s index() method.

// UserController.php

public function index()
{
        return 'Yes!! Admin namespace is working successfully';
}

Step 2: Define group() function.

To write Admin namespace routes, open routes >> web.php file and add the following code.

// web.php

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

Here, we assign a namespace Admin to the route users. You can here see that we have grouped the routes using the group() method.

We can also write this code in a better way, like the following.

// web.php

Route::group(['namespace' => 'Admin'], function() {
    Route::resource('users', 'UserController');
});

Here, we have used the group() function, which takes two parameters, as mentioned in the syntax.

  1. array
  2. callback function

The first parameter is an associative array containing namespace, prefix, or middleware for the group of routes. Save the file and go to the http://localhost:8000/users. You will see `Yes!! Admin namespace is working successfully`. 

Here, you can add as many routes as possible, which are categorized under the Admin namespace.

// web.php

Route::group(['namespace' => 'Admin'], function() {
    Route::resource('users', 'UserController');
    Route::resource('sales', 'SalesController');
    Route::resource('marketings', 'MarketingController');
});

Laravel 8 Path Prefixes

Path prefix is useful when we want to provide a common URL structure. For example, in our application, we are creating functionality for the admin module that means we can create a common prefix admin for all of our routes under admin functionality.

To add path prefix, add one more item in the associative array parameter of the route group called prefix as key and admin as value.

// web.php

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {
    Route::resource('users', 'UserController');
});

Now, your URL should be http://localhost:8000/admin/users and not http://localhost:8000/users.

Laravel 8 Middleware

Middleware provides a convenient mechanism for filtering HTTP requests entering your application. We can also assign middleware to all the routes within a group. We can add one more property to the associative array whose key is middleware, and value is ‘middleware name’. 

In this example, we will use auth middleware, so type the following command in your terminal to scaffold the auth module provided by Laravel.

php artisan ui:auth

Now we want to protect the admin routes so that we will use auth middleware to admin routes. To do that, write the following code inside the web.php file.

// web.php

Route::group(['namespace' => 'Admin', 
            'prefix' => 'admin',
            'middleware' => 'auth'], function() {
    Route::resource('users', 'UserController');
});

Now, our admin routes are protected, and if you are logged in to the application then and then you will be able to see the content. Otherwise, you will be redirected to the login page.

Go to this URL: http://localhost:8000/admin/users, and you will be redirected to the http://localhost:8000/ login page. So this is how you can multiple routes and assign them to a common group of the route which shares the namespaceprefix, and middleware.

Reactions

Post a Comment

1 Comments

  1. 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.

    ReplyDelete

CAN FEEDBACK

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)

close