🚦 Laravel Routing Step-by-Step Guide (With Examples)

In Laravel, routing is the heart of your application. It determines how URLs map to controllers or closures and defines the behavior for every request. This guide walks you through the fundamentals of Laravel routing, step by step.


✅ Step 1: Routing Basics

In Laravel, a route is a URL path that leads to a specific logic or controller. For example, when a user visits yourapp.com/about, Laravel needs to decide what to do — show a view, call a controller, or return JSON.

📌 Syntax Example:

Route::get('/about', function () { return view('about'); });

✅ Step 2: Route Files

Laravel organizes its routes into different files:

  • routes/web.php → for browser-based (web) routes
  • routes/api.php → for API endpoints (JSON response)
  • routes/console.php → for Artisan CLI routes
  • routes/channels.php → for broadcasting channels

📁 For most web apps, you’ll edit routes/web.php.


✅ Step 3: Defining Routes

Laravel uses different HTTP verbs:

  • Route::get() – Retrieve data
  • Route::post() – Submit data
  • Route::put() / Route::patch() – Update data
  • Route::delete() – Delete data

Example:

Route::get('/users', function () {
return 'All Users'; }); Route::post('/users', function () { return 'Create User'; });

✅ Step 4: Route Parameters

You can make routes dynamic using parameters:

Route::get('/users/{id}', function ($id) { return "User ID: $id"; });

You can also make them optional:

Route::get('/users/{name?}', function ($name = 'Guest') {
return "Welcome, $name"; });

✅ Step 5: Route Naming

You can assign names to routes using the name() method. This is useful for redirects or generating URLs.

Route::get('/dashboard', function () { return view('dashboard'); })->name('dashboard'); $url = route('dashboard'); // Generates /dashboard

✅ Step 6: Route Groups

Route groups allow you to group multiple routes under shared settings like middleware, prefix, or namespace.

Example: Group with prefix and middleware

Route::prefix('admin')->middleware('auth')->group(function () {
Route::get('/dashboard', function () { return 'Admin Dashboard'; }); Route::get('/users', function () { return 'Admin Users'; }); });

✅ Step 7: Route Caching (Performance Boost)

When your app is ready for production, you can cache your routes for better performance:

php artisan route:cache

To clear cached routes:

php artisan route:clear

✅ Step 8: Controller Routes

Instead of defining logic in closures, Laravel promotes using controllers:

Route::get('/users/{id}', [UserController::class, 'show']);

This will call the show() method in UserController.


✅ Step 9: Route Model Binding (Automatic Querying)

Laravel automatically binds route parameters to Eloquent models using Route Model Binding.

Route::get('/users/{user}', [UserController::class, 'show']);

If you define the controller method like this:

public function show(User $user)
{ return $user; }

Laravel will fetch the user record based on the ID in the URL.


🧠 Summary of Laravel Routing

Feature Description
Route::get() Handles GET requests
Route Parameters Allows dynamic routes like /users/{id}
Named Routes Reference routes using names like route('home')
Route Groups Apply shared middleware or prefixes
Controllers Organize logic using controller classes
Model Binding Automatically resolve Eloquent models
Route Caching Speeds up routing in production

Post a Comment

Previous Post Next Post

Contact Form