When you are creating any web application in Laravel, you needed more than one route. These routes will redirect the request to the specific function. Mostly, these functions are landed in a controller. When you have multiple routes which are linked with the same controller then you call that controller name again and again. But it is an old way and a bad practice to repeat the controller name again and again while defining the routes. Also, it will make your code lengthy. So, how we can reduce this code length and make it a bit short?
No idea? No worries, I will show you here in this post by using the Laravel Route group. You are already familiar with the Laravel route group things. But, let’s see how we can use it in Laravel 9 after the updates.
Prerequisites
We will be implementing this functionality in Laravel 9. So, if you are with the previous version of Laravel then please upgrade it first. For creating a Laravel 9 project, you will need to have the following configurations.
- PHP >=8.0.2
- Composer
- Apache/Nginx Server
- VS Code Editor (Optional)
- MySQL (version > 5)
I am assuming you are ready with a Laravel 9 project setup. If not then let’s create a new project setup.
How to Clear Application Cache without Command Line in Laravel
Create Project For Implementing Laravel Route Group
Open the terminal or command prompt and hit the below command.
composer create-project --prefer-dist laravel/laravel myapp
Once, you have the project set up, let’s move to the next step of this post.
Create a Controller in Laravel 9 For Route Group
For implementing the Route group for the controller based, we need at least one controller. So, that we can create a couple of routes associated with this controller.
So, let’s create a controller first.
php artisan make:controller WebController
The above command will create a controller named WebController.php. After having this controller, let’s create a few functions inside this.
<?php
namespace App\Http\Controllers;
class WebController extends Controller
{
/**
* Function for home
* @param NA
* @return message
*/
public function homeFunction()
{
return "<h1> This is a home function </h1>";
}
/**
* Function for about
* @param NA
* @return message
*/
public function aboutFunction()
{
return "<h1> This is a about function </h1>";
}
/**
* Function for contact
* @param NA
* @return message
*/
public function contactFunction()
{
return "<h1> This is a contact function </h1>";
}
}
In the above controller, I’ve created three functions and returned a simple message.
Now, let’s come to the routing part so that we can call these methods.
How to Clear Application Cache without Command Line in Laravel
Add Routes in Laravel 9
For calling these methods in the route, we can use two different ways. The first way is the old one as shown below.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WebController;
Route::get('home', [WebController::class, 'homeFunction']);
Route::get('about-us', [WebController::class, 'aboutFunction']);
Route::get('contact', [WebController::class, 'contactFunction']);
We were using this approach in Laravel 8 by writing the controller name again and again.
But, now, in the Laravel 9, this is enhanced a little bit as shown below.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WebController;
Route::controller(WebController::class)->group(function() {
Route::get('home', 'homeFunction');
Route::get('about-us', 'aboutFunction');
Route::get('contact', 'contactFunction');
});
Here, in the above routing approach, we grouped all the routes belonging to the same controller. So, here, we don’t need to put the controller name again and again for the related controller.
Now, you can run and check the results.
Similarly, you will have the about and contact page results.
So, that’s it for this post, I hope this will help you in optimizing the routes and code base.
Leave a Reply