Group | EBTA

Laravel Route Groups

Zubair Idris Aweda

--

In this article, you’ll learn about Laravel route groups, how to use them, and why use them.

To get started, you should:

  • Know basic routing in Laravel using closures or controllers, or learn about it by reading Routing In Laravel 8.
  • Have a basic understanding of what middlewares are.

Getting Started

Quite a lot of times in our web applications, we define a lot of routes (depending on the size of our application though) that share common features (the same path prefix, or the same name prefix, or even other middleware or authentication). Defining these routes individually (and repeating every feature they share upon every definition) could be tiring and could get hard to manage later on.

Laravel makes it very easy to define, respond to and perform more actions with these routes by letting us group them (based on what they have in common) and performing the same actions on all of them. Management becomes super easy if they are all together as one. 😄

You’re excited already? Or aren’t you? Well, I am. Let’s dive in.

Dive In | Clip Art Library

Route Groups

Route groups let you group routes that share route attributes such as path, name, middleware to avoid having to define these attributes on each individual route.

Creating A Basic Route Group

A basic route group in Laravel is created using Route::group() as shown below.

The method takes in two parameters, an array, and a callback. The array contains all shared features of the routes in the group. An example can be seen below.

Here, all the routes in this group are prefixed with the /admin path and also prefixed with the admin name. The callback contains all routes in the group. So if a route is defined like this:

It actually refers to the /admin/ path and can be accessed with the name “admin.index”.

For as many routes as you can include in the group, they all inherit these rules. And now you have time to worry about other things. 😊

Creating A Middleware Route Group

Creating a route group to share one or more common middleware can be achieved in two ways.

A. By Passing The Middleware Into The Features Array

B. By Using The Middleware Before Defining The Group

In both methods shown above, every route in the group will require the auth middleware instead of it being defined in every controller.

Creating A Path Route Group

Creating a path route group has two methods, like the middleware route group.

A. By Passing The Path (prefix) Into The Features Array

B. By Using The Prefix Before Defining The Group

In both methods, the /admin path is matched. All routes here forth created will be prefixed with the /admin path.

Creating A Name Route Group

A. By Passing The Name (as) Into The Features Array

B. By Using The Name Before Defining The Group

When specifying the name, always add the . after the name, as the name is put in front of whatever name all the routes in the group have as it was given.

Summary

Laravel route groups really save a lot of stress, and repetition in code, without losing code neatness. To learn more on route groups, read here.

The End

If you have any questions or relevant great advice, please leave them in the comments section.

To read more of my articles or follow my works, you may connect with me on LinkedIn and Twitter. It’s quick, it’s easy, and it’s free!

--

--

Zubair Idris Aweda

Software Engineer | PHP | Javascript. Technical Writer