Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Grouping routes by controllers in Laravel 8.x

There are many ways using which you can group your routes in Laravel right now. For instance, as by middlewares, subdomains, prefixes to name a few.

But there’s one more scenario, that can be covered, that was missing.

Consider the following group of routes.

use Illuminate\Support\Facades\Route;

Route::get('/', [BookController::class, 'index']);
Route::get('/books/{id}', [BookController::class, 'show']);
Route::get('/books/search/{search}', [BookController::class, 'search']);
Route::post('/book', [BookController::class, 'store']);

As you can tell, we have four routes to manage books and if you notice, all of these routes have one thing in common. i.e. the BookController.

What if we could just group these routes by the controller? The routes would become more manageable and concise. No?

Well, a recent PR for Laravel 8.x attempts to solve this.

Group by Controller

According to this PR, the framework now comes with a controller method (that you can call on the Illuminate\Support\Facades\Route facade) using which you can group the routes which are using the same controller.

So, if we want to rewrite the previous example using this approach, here’s how we can do it.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BookController;

Route::controller(BookController::class)->group(function () {
    Route::get('/', 'index');
    Route::get('/books/{id}', 'show');
    Route::get('/books/search/{search}', 'search');
    Route::post('/book', 'store');
});

As you can tell, by using this approach now you have all the routes, that use the same controller, are under the same group. Now, you only need to provide the controller method that they are invoking.

Falling back

Now, if you’re grouping routes by controllers using the above approach and if you specify the controller on the route once again, it will override the group controller.

Consider the following for example.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BookController;

Route::controller(BookController::class)->group(function () {
    Route::get('/books/{id}', 'EbookController@show');
});

As you can tell, we specified the EbookController on the route definition once again. So, this will take the priority over the BookController in this case.

It’s the same for other syntaxes of specifying controllers as well such as an array, closure syntax, and invokable controllers.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?