Laravel Routing

Laravel routing maps URLs to the code that handles them. Every page and API endpoint in your app starts with a route, defined in routes/web.php for browser pages or routes/api.php for APIs.

Basic Routes

Routes exist for each HTTP verb. The second argument is a closure or a controller action.

<?php
use Illuminate\Support\Facades\Route;

Route::get('/', fn () => view('welcome'));
Route::view('/about', 'about');
Route::post('/contact', [ContactController::class, 'store']);
Route::redirect('/home', '/');
GET  /         -> welcome view
GET  /about    -> about view
POST /contact  -> ContactController@store
GET  /home     -> 302 redirect to /

Laravel Route Parameters

Wrap a segment in braces to capture it. Add ? for optional parameters and constrain them with where helpers.

<?php
Route::get('/users/{id}', fn (int $id) => "User #$id")
    ->whereNumber('id');

Route::get('/blog/{slug?}', fn (?string $slug = null) => $slug ?? 'All posts');
GET /users/15         -> User #15
GET /users/abc        -> 404 Not Found
GET /blog             -> All posts
GET /blog/first-post  -> first-post

Named Routes

Give routes names and generate URLs with route(). If the URL changes later, links across your app keep working.

<?php
Route::get('/dashboard/profile', [ProfileController::class, 'show'])
    ->name('profile.show');

// In PHP or Blade:
echo route('profile.show');
// return redirect()->route('profile.show');
http://localhost:8000/dashboard/profile

Route Groups

Groups share attributes such as a URL prefix, name prefix or middleware.

<?php
Route::middleware('auth')->prefix('admin')->name('admin.')->group(function () {
    Route::get('/users', [Admin\UserController::class, 'index'])->name('users');
    Route::get('/reports', [Admin\ReportController::class, 'index'])->name('reports');
});
GET /admin/users    name: admin.users    (requires login)
GET /admin/reports  name: admin.reports  (requires login)

Resource Routes and Route Model Binding

Route::resource creates the seven standard CRUD routes. When a parameter is type-hinted with a model, Laravel loads the record automatically and returns 404 if it does not exist.

<?php
Route::resource('posts', PostController::class);

// Route model binding
Route::get('/p/{post}', fn (App\Models\Post $post) => $post->title);
GET       /posts               posts.index
GET       /posts/create        posts.create
POST      /posts               posts.store
GET       /posts/{post}        posts.show
GET       /posts/{post}/edit   posts.edit
PUT/PATCH /posts/{post}        posts.update
DELETE    /posts/{post}        posts.destroy

Listing Routes

Run php artisan route:list to see every registered route, its name, middleware and handler. It is the quickest way to debug a "404" or "route not defined" error.

Frequently asked questions

Where is routes/api.php in Laravel 11?

It is not included by default. Run php artisan install:api to create it and install Sanctum.

What is route model binding in Laravel?

When a route parameter is type-hinted with an Eloquent model, Laravel automatically fetches the matching record by ID (or another key) and returns 404 if not found.

How do I generate a URL for a named route?

Use route("name", $params) in PHP or {{ route("name") }} in Blade.