Skip to content
Reactjs Laravel

Laravel Routing Explained: A Complete Guide with Examples

Raheel Shan

Raheel Shan

March 6th, 2025 · 11 min read

Laravel Routing Explained: A Complete Guide with Examples

Routing in Laravel means mapping a URL and an HTTP method to the code that answers it. You define routes in routes/web.php (or routes/api.php) with the Route facade, give them names, group them to share middleware and prefixes, and bind URL segments to models. This guide covers every part of Laravel routing you will use day to day, with code you can paste into a project.

Quick answer

  1. Define a route with Route::get('/users', ...) (or post, put, patch, delete).
  2. Name it with ->name('users.index') and link to it with the route() helper, never a hard-coded URL.
  3. Share middleware, prefixes and names with Route::middleware(), prefix() and name() groups.
  4. Use Route::domain() for subdomains, Route::redirect() for redirects and Route::fallback() for custom 404 pages.
  5. Type-hint a model in the route closure and Laravel finds it for you (route model binding).

HTTP verb routes

Each HTTP verb has its own method:

Route::get('/users', $callback);
Route::post('/users', $callback);
Route::put('/users/{id}', $callback);
Route::patch('/users/{id}', $callback);
Route::delete('/users/{id}', $callback);
Route::options('/users', $callback);

To answer several verbs with one callback, use match. any answers all of them:

Route::match(['get', 'post'], '/users/{id}', function ($id) {
    // handling logic
});

Route::any('/users', function () {
    // handling logic
});

Prefer explicit verbs over Route::any(). It keeps the route's purpose clear and does not expose methods you never meant to accept.

Named routes and the route() helper

A named route gives a URL a stable name. The route() helper turns that name back into a URL. This is called reverse routing, and it is the main reason to name routes.

Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

// In a controller
return redirect()->route('dashboard');

// In Blade
<a href="{{ route('dashboard') }}">Dashboard</a>

Pass parameters as an array. Extra keys become the query string:

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

route('users.show', ['user' => 5]);              // /users/5
route('users.show', ['user' => 5, 'tab' => 'posts']); // /users/5?tab=posts

Why not url('/users/5')? If you rename the URL later, every url() call breaks silently. route() follows the name, and it throws an exception if the name does not exist, so a typo fails loudly. Keep url() for files and assets. The 3 powerful Laravel routing techniques post covers this comparison in more detail.

Laravel redirect routes: 301, 302 and external URLs

For a redirect that needs no logic, skip the controller:

Route::redirect('/here', '/there');            // 302 (temporary)
Route::redirect('/here', '/there', 301);       // custom status code
Route::permanentRedirect('/old', '/new');      // always 301

Use a 302 when the redirect may change, such as a seasonal sale page. Use a 301 when the old URL is gone for good, for example a renamed blog post. A 301 tells search engines to move the old URL's ranking to the new one.

Laravel redirect to an external URL

Inside a controller, use redirect()->away() for an address outside your app. The target can also be passed straight to redirect():

return redirect()->away('https://example.com/docs');
return redirect('https://example.com/docs', 301);

Route::redirect() also accepts a full URL as the second argument. Never redirect to a URL taken from user input without checking it against an allowed list, or you create an open redirect.

View, controller and model routes

Route::view() returns a view without a controller. It is meant for static pages:

Route::view('/welcome', 'welcome', ['name' => 'John']);

Route::controller() removes the repeated class name from a group of routes. You still name each method yourself:

Route::controller(UserController::class)->group(function () {
    Route::get('/users', 'index');
    Route::post('/users', 'store');
    Route::get('/users/{user}', 'show');
});

For full CRUD, Route::resource('users', UserController::class) registers all seven standard routes at once. See Laravel CRUD like a pro for a reusable setup.

Route model binding

Type-hint a model, and Laravel loads it from the URL segment. If the record does not exist, the visitor gets a 404:

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

This is implicit binding: the {user} name must match the variable name. To look up by another column, name it in the route:

Route::get('/posts/{post:slug}', function (Post $post) {
    return $post;
});

When a route has two bound models and the second is a child of the first, scoped bindings make Laravel search only inside the parent's relationship. With a custom key on the child, it happens automatically. Without one, ask for it:

Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
    return $post; // only a post that belongs to this user
})->scopeBindings();

You can also apply scopeBindings() to a whole group, or turn it off with withoutScopedBindings().

For full control, bind explicitly in boot() of AppServiceProvider:

Route::model('user', User::class);

Route::bind('user', function (string $value) {
    return User::where('email', $value)->firstOrFail();
});

In an API, return an Eloquent API resource instead of the raw model, so you do not expose your table structure.

Laravel route domain and subdomain routing

Use Route::domain() to send different hosts to different routes:

Route::domain('admin.mysite.com')->group(function () {
    Route::get('/dashboard', fn () => 'Admin Dashboard');
});

Route::domain('mysite.com')->group(function () {
    Route::get('/', fn () => view('home'));
});

A domain can contain a wildcard parameter, handy for multi-tenant apps:

Route::domain('{account}.mysite.com')->group(function () {
    Route::get('/dashboard', function (string $account) {
        return "Dashboard of {$account}";
    });
});

Register subdomain routes before root-domain routes. Otherwise a root route with the same path can overwrite the subdomain one. For larger apps, keep each domain in its own file (routes/admin.php) and load it from web.php with require. To see how a request reaches your app by host in the first place, read The Journey of a Web Request: Domains.

Laravel fallback route

Route::fallback() runs when no other route matches, so you can serve a custom 404 page:

Route::fallback(function () {
    return response()->view('errors.404', [], 404);
});

Keep the status code. Returning a view without 404 answers "200 OK", and crawlers treat a dead link as a real page. Define the fallback last in routes/web.php.

Route groups: middleware, prefix and name

Groups stop you repeating yourself. Each of these can be chained:

Route::middleware(['auth', 'verified'])->group(function () {
    // protected routes
});

Route::prefix('admin')->group(function () {
    Route::get('/users', $callback); // /admin/users
});

Route::name('admin.')->group(function () {
    Route::get('/users', $callback)->name('users'); // admin.users
});

Combine them for a clean admin area:

Route::middleware('auth')->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/users', [UserController::class, 'index'])->name('users');
});
// URLs: /admin/dashboard, /admin/users
// Names: admin.dashboard, admin.users

Note the trailing dot in name('admin.'). The prefix is joined to the route name as plain text, so without the dot you get adminusers. Prefixes and names nest, so prefix('v1') inside prefix('admin') gives /v1/admin/....

Patterns and the current route

Route::pattern() limits a parameter everywhere. Define it in boot():

Route::pattern('id', '[0-9]+');

To inspect the route handling the request:

$route  = Route::current();            // Illuminate\Routing\Route
$name   = Route::currentRouteName();   // string
$action = Route::currentRouteAction(); // string

When not to use these features

  • Do not put business logic in route closures. Closures cannot be cached by route:cache; use controllers in production.
  • Do not use Route::view() for pages that need data processing.
  • Do not rely on a fallback route to hide missing routes. Fix the broken links.

FAQ

What is routing in Laravel? Routing maps a URL and HTTP method to a closure or controller method. Routes live in routes/web.php and routes/api.php.

What is reverse routing in Laravel? It means generating a URL from a route name with route('name', $params), instead of writing the URL by hand.

How do I do a 301 redirect in Laravel? Use Route::permanentRedirect('/old', '/new'), or Route::redirect('/old', '/new', 301). In a controller, return redirect('/new', 301);.

How do I redirect to an external URL in Laravel? Return redirect()->away('https://example.com') from a controller, or give a full URL to Route::redirect().

How do I create a Laravel fallback route? Add Route::fallback(function () { ... }) at the end of routes/web.php. It runs when nothing else matches.

Structure your Laravel app with me

Fat controllers, business logic in five places, views that accept any array. Bring your codebase or your plan.

In 60 minutes I review how it is laid out and we settle modules, DTOs, ViewModels and clean controllers.

Book a Laravel Architecture call

Found this useful?

Support the work that goes into these posts.

Support