Simplify your Laravel routes

Table of contents
In Laravel Framework provides some awesome features, one of which is the route.
Laravel fundamentally provides developers to define how the application response in various HTTP request. When you install a Laravel project, you get routes/web.php file for web routes.
Basic Routes
This is a basic route to call a view file. this is provided by laravel.
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
If you want to connect with a controller, you can call a controller function using the route, pass a parameter, and check with middleware.
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PostController;
Route::get('/home', [HomeController::class, 'index']); //call index function
Route::get('/home', [HomeController::class, 'index'])->middleware('auth'); //check with middleware
Route::get('/posts/{id}', [PostController::class, 'show']); //passing a paramiter
Name Routes
Laravel provides a global helper function name route. This helps to call the URL using a defined name, and handle to pass parameters.
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
In this route, I define a name, and I can use it anywhere to call this route using this name. like:
// In balde file
<a href="{{ route('dashboard') }}">Dashboard</a>
// In controller
$url = route('dashboard');
// Redirect
return redirect()->route('dashboard');
Invokable Controller Route
Previously, we saw route call a controller function, but in an Invokable controller, we only call the controller
use App\Http\Controllers\ContactController;
Route::post('/contact', ContactController::class)->name('contact.submit');
Grouping Route
Laravel allows you to group multiple routes and apply shared attributes like middleware, prefix, namespace, or name prefix to reduce repetition and improve organization.
//grouping with middleware
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index']);
Route::get('/settings', [SettingsController::class, 'index']);
});
//grouping with prefix
Route::prefix('admin')->group(function () {
Route::get('/users', [AdminController::class, 'users']);
Route::get('/settings', [AdminController::class, 'settings']);
});
But when you're working on a big project with a team, that’s the time to write a readable and organize your code.
This is one of the examples of how you can write a simple and readable route
//user routes
Route::group([
'middleware' => ['auth'],
'prefix' => 'users',
'as' => 'users.',
'controller' => UserController::class,
],
function () {
Route::get('/', 'index')->name('index');
Route::get('/create', 'create')->name('create');
Route::post('/', 'store')->name('store');
Route::get('/{user}', 'show')->name('show');
Route::get('/{user}/edit', 'edit')->name('edit');
Route::put('/{user}', 'update')->name('update');
Route::delete('/{user}', 'destory')->name('destroy');
});
Subscribe to my newsletter
Read articles from Aniruddha Mandal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
