• Fri, Mar 2026

This in-depth guide covers everything you need to know about Laravel routing. We’ll explore how routes work, how to define them, pass parameters, use controllers, and group routes with middleware. By the end, you’ll know how to build clean, flexible, and maintainable URLs in Laravel applications. Perfect for beginners and intermediate developers who want a professional step-by-step tutorial.

Introduction

When building web applications, one of the most important aspects is defining how users access different parts of your app. In Laravel, this is handled through routing. Routing maps user requests (like visiting /home) to the appropriate response, such as returning a view, running a controller method, or sending JSON data.

Laravel offers one of the cleanest and most powerful routing systems in the PHP world. It is flexible, expressive, and developer-friendly, allowing you to easily define routes for web pages, APIs, and even command-line applications.

What is Routing in Laravel?

Routing is the process of defining how Laravel should respond to different HTTP requests. For example, when a user types https://yourapp.com/about in their browser, Laravel decides what should happen based on the rules you define in your route files.

Laravel routes are defined inside the routes/ directory. The most common file is routes/web.php for web routes, and routes/api.php for API routes.

Basic Routing

Defining a Simple Route

A basic Laravel route looks like this:


// routes/web.php
use Illuminate\Support\Facades\Route;

Route::get('/hello', function () {
    return "Hello, Laravel!";
});
        

When you visit http://localhost:8000/hello, Laravel will return the string "Hello, Laravel!".

Different HTTP Verbs

Laravel allows routes for different HTTP methods like GET, POST, PUT, PATCH, and DELETE.


// GET request
Route::get('/users', function () {
    return 'List of users';
});

// POST request
Route::post('/users', function () {
    return 'User created';
});

// PUT request
Route::put('/users/{id}', function ($id) {
    return "Update user {$id}";
});

// DELETE request
Route::delete('/users/{id}', function ($id) {
    return "Delete user {$id}";
});
        

Route Parameters

Laravel routes often require parameters to pass dynamic values in the URL. For example, showing a user profile with an ID.

Required Parameters


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

If you visit /user/10, it will display "User ID: 10".

Optional Parameters


Route::get('/user/{name?}', function ($name = 'Guest') {
    return "Hello, " . $name;
});
        

Visiting /user will show "Hello, Guest". Visiting /user/John will show "Hello, John".

Regular Expression Constraints


Route::get('/product/{id}', function ($id) {
    return "Product ID: " . $id;
})->where('id', '[0-9]+');
        

This ensures only numeric values are accepted for the id parameter.

Named Routes

Laravel lets you assign names to routes, which makes generating URLs easier and less error-prone.


Route::get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');

// Generating URLs
$url = route('dashboard');

// Redirecting
return redirect()->route('dashboard');
        

Route Groups

Grouping routes allows you to apply common settings like middleware, prefixes, or namespaces to multiple routes.

Using Prefix


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

    Route::get('/users', function () {
        return 'Manage Users';
    });
});
        

This creates routes /admin/dashboard and /admin/users.

Using Middleware


Route::middleware(['auth'])->group(function () {
    Route::get('/profile', function () {
        return 'User Profile';
    });

    Route::get('/settings', function () {
        return 'Account Settings';
    });
});
        

Routes with Controllers

Instead of writing logic inside closures, Laravel encourages using controllers for cleaner code.


// routes/web.php
use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);
        

Controller Example:


// app/Http/Controllers/UserController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index() {
        return "Showing all users";
    }
}
        

Route Caching for Performance

In production, you can cache your routes to make your application faster.


php artisan route:cache
php artisan route:clear
        

This significantly reduces route registration time on large applications.

Comparison of Route Features

Here’s a quick comparison table for common routing features:

FeatureExampleUsage
Basic RouteRoute::get('/home')Define static pages
Route ParameterRoute::get('/user/{id}')Dynamic values in URL
Optional Parameter{name?}Default values
Named Route->name('route')Easy referencing
Route GroupRoute::prefix('admin')Shared prefix or middleware
Controller Route[Controller::class, 'method']Better structure

Conclusion

Routing in Laravel is both simple and powerful. You can start with basic routes and gradually add complexity using parameters, groups, middleware, and controllers. Mastering routing will allow you to create clean, SEO-friendly, and scalable URLs that enhance both developer productivity and user experience.

By following this tutorial, you should now have a strong foundation in Laravel routing and be ready to build modern, maintainable applications with confidence.

This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy