Routing in Laravel is one of the most essential parts of building a web application. It connects your application’s URLs to the logic defined in controllers or closures. Whether you're building a simple blog or a complex API, understanding Laravel routing is a must.
๐ What is Routing in Laravel?
Routing in Laravel allows you to define URL patterns and map them to specific functionality. These routes are usually defined in the routes/web.php or routes/api.php files depending on the type of application.
๐ Defining Basic Routes
GET Route
Route::get('/home', function () {
    return view('home');
});
  
  POST Route
Route::post('/submit', [FormController::class, 'store']);
  
  ๐ฆ Route Parameters
Required Parameters
Route::get('/user/{id}', [UserController::class, 'show']);
  
  Optional Parameters
Route::get('/user/{name?}', function ($name = 'Guest') {
    return "Hello, " . $name;
});
  
  ๐ Named Routes
Route::get('/dashboard', [UserController::class, 'dashboard'])->name('dashboard');
  
  ๐ฅ Route Groups with Middleware or Prefix
Route::middleware(['auth'])->prefix('admin')->group(function () {
    Route::get('/dashboard', [AdminController::class, 'index']);
    Route::get('/users', [AdminController::class, 'users']);
});
  
  ๐งฉ Controller Route Grouping
Instead of repeating controller names for each route, Laravel allows you to group routes by controller using Route::controller().
use App\Http\Controllers\UserController;
Route::controller(UserController::class)->group(function () {
    Route::get('/users', 'index');
    Route::get('/users/{id}', 'show');
    Route::post('/users', 'store');
});
  
  This is helpful for APIs or dashboards where routes share the same controller and reduces repetition.
⚙️ Resource Routes
Route::resource('posts', PostController::class);
  
  ๐งฉ API Resource Routes
Route::apiResource('products', ProductController::class);
  
  ๐ Route View and Redirect
Route View
Route::view('/about', 'about');
  
  Route Redirect
Route::redirect('/old-page', '/new-page');
  
  ๐ก Fallback Route
Route::fallback(function () {
    return response()->view('errors.404', [], 404);
});
  
  ๐ Route Middleware
Route::get('/profile', [UserController::class, 'profile'])->middleware('auth');
  
  ๐ Match and Any Methods
Match Multiple HTTP Methods
Route::match(['get', 'post'], '/contact', [ContactController::class, 'handle']);
  
  Accept Any HTTP Method
Route::any('/webhook', [WebhookController::class, 'receive']);
  
  ✅ Route Constraints
Route::get('/user/{id}', [UserController::class, 'show'])->where('id', '[0-9]+');
  
  ๐ Signed Routes
Route::get('/verify/{id}', [VerifyController::class, 'verify'])->name('verify')->middleware('signed');
  
  ๐ ️ Artisan Command for Listing Routes
php artisan route:list
  
  ๐ Final Thoughts
Laravel’s routing system is powerful, clean, and developer-friendly. Whether you're building small features or large enterprise applications, mastering routing will speed up your development and keep your code organized.
๐ Learn more in the official Laravel docs: laravel.com/docs/routing
Comments
Post a Comment