> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Routing

> How to define routes, use route parameters, named routes, route groups, and resource routes in Laravel.

## Basic routing

The most basic Laravel route accepts a URI and a closure, letting you define a route and its behavior without complex configuration files:

```php theme={null}
use Illuminate\Support\Facades\Route;

Route::get('/greeting', function () {
    return 'Hello World';
});
```

### Default route files

All Laravel routes are defined in the files inside the `routes/` directory. Laravel automatically loads these files based on the configuration in `bootstrap/app.php`.

```mermaid theme={null}
flowchart LR
    A["Incoming request"] --> B{{"Request type"}}
    B -->|"Browser<br>Session / CSRF"| C["routes/web.php<br>web middleware group"]
    B -->|"API<br>Stateless"| D["routes/api.php<br>api middleware group"]
    B -->|"CLI command"| E["routes/console.php<br>Artisan command definitions"]
    C --> F["Controller / Closure"]
    D --> F
    E --> G["Command handler"]
```

Define browser-facing routes in `routes/web.php`. Routes in this file have the `web` middleware group applied, which provides session handling, CSRF protection, and cookie encryption:

```php theme={null}
use App\Http\Controllers\UserController;

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

### API Routes

If your application will also offer a stateless API, you may enable API routing using the `install:api` Artisan command:

```shell theme={null}
php artisan install:api
```

The `install:api` command installs [Laravel Sanctum](/en/sanctum), which provides a robust, yet simple API token authentication guard which can be used to authenticate third-party API consumers, SPAs, or mobile applications. In addition, the `install:api` command creates the `routes/api.php` file:

```php theme={null}
Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware('auth:sanctum');
```

Of course, you are free to omit the `auth:sanctum` middleware on routes that should be publicly accessible.

The routes in `routes/api.php` are stateless and are assigned to the `api` [middleware group](/en/middleware#laravels-default-middleware-groups). Additionally, the `/api` URI prefix is automatically applied to these routes, so you do not need to manually apply it to every route in the file. You may change the prefix by modifying your application's `bootstrap/app.php` file:

```php theme={null}
->withRouting(
    api: __DIR__.'/../routes/api.php',
    apiPrefix: 'api/admin',
    // ...
)
```

### Available HTTP methods

The router can register routes that respond to any HTTP verb:

```php theme={null}
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
```

To respond to multiple HTTP verbs, use `match` or `any`:

```php theme={null}
Route::match(['get', 'post'], '/', function () {
    // ...
});

Route::any('/', function () {
    // ...
});
```

<Info>
  HTML forms in the `web` routes file that send `POST`, `PUT`, `PATCH`, or `DELETE` requests must include the `@csrf` Blade directive. Requests without it will be rejected.
</Info>

## Returning a view

When a route only needs to return a view, use `Route::view` for a concise definition:

```php theme={null}
Route::view('/welcome', 'welcome');

// Passing data to the view
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
```

## Route parameters

### Required parameters

Capture segments of the URI by defining route parameters wrapped in `{}`. The captured values are passed as arguments to the route callback:

```php theme={null}
Route::get('/user/{id}', function (string $id) {
    return 'User ID: ' . $id;
});
```

You can define multiple parameters:

```php theme={null}
Route::get('/posts/{post}/comments/{comment}', function (string $postId, string $commentId) {
    return "Post {$postId}, comment {$commentId}";
});
```

<Warning>
  Route parameters cannot contain the `/` character.
</Warning>

### Optional parameters

Make a parameter optional by adding `?` after its name and providing a default value for the corresponding argument:

```php theme={null}
Route::get('/user/{name?}', function (?string $name = null) {
    return $name ?? 'Guest';
});

Route::get('/user/{name?}', function (?string $name = 'Guest') {
    return $name;
});
```

### Regular expression constraints

Constrain the format of a route parameter using the `where` method:

```php theme={null}
Route::get('/user/{id}', function (string $id) {
    // ...
})->where('id', '[0-9]+');

Route::get('/user/{name}', function (string $name) {
    // ...
})->where('name', '[a-zA-Z]+');
```

Commonly used patterns have convenience methods:

```php theme={null}
Route::get('/user/{id}/{name}', function (string $id, string $name) {
    // ...
})->whereNumber('id')->whereAlpha('name');
```

## Named routes

Named routes let you reference a route by name when generating URLs or redirects, so you don't need to hard-code URLs:

```php theme={null}
Route::get('/user/profile', function () {
    // ...
})->name('profile');
```

### Generating URLs to named routes

Use the `route` helper to generate a URL from a route name:

```php theme={null}
// Generate a URL
$url = route('profile');

// Generate a URL with parameters
$url = route('user.show', ['id' => 1]);
```

### Redirecting to named routes

```php theme={null}
return redirect()->route('profile');
```

## Route groups

Group related routes to share middleware, controllers, prefixes, and other attributes without repeating them on each route.

```mermaid theme={null}
flowchart TD
    A["HTTP request"] --> B["Router"]
    B --> C{{"Matching route<br>found?"}}
    C -->|"No"| D["404 response"]
    C -->|"Yes"| E["Check route middleware"]
    E --> F{{"Middleware<br>passed?"}}
    F -->|"Rejected"| G["Redirect /<br>error response"]
    F -->|"Passed"| H["Controller /<br>closure executed"]
    H --> I["Response generated"]
    I --> J["Sent to client"]
```

### Middleware

```php theme={null}
Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', function () {
        // Only accessible to authenticated users
    });

    Route::get('/account', function () {
        // Only accessible to authenticated users
    });
});
```

### Controllers

Group routes that share the same controller:

```php theme={null}
use App\Http\Controllers\OrderController;

Route::controller(OrderController::class)->group(function () {
    Route::get('/orders', 'index');
    Route::post('/orders', 'store');
    Route::get('/orders/{id}', 'show');
});
```

### Prefixes

Add a common URI prefix to a group of routes:

```php theme={null}
Route::prefix('admin')->group(function () {
    Route::get('/users', function () {
        // Accessible at /admin/users
    });
    Route::get('/posts', function () {
        // Accessible at /admin/posts
    });
});
```

### Name prefixes

Prefix the name of every route in a group:

```php theme={null}
Route::name('admin.')->group(function () {
    Route::get('/users', function () {
        // Route named "admin.users"
    })->name('users');
});
```

## Resource routes

For controllers that handle CRUD operations on a resource, `Route::resource` registers all the standard routes in one line:

```php theme={null}
use App\Http\Controllers\PhotoController;

Route::resource('photos', PhotoController::class);
```

This single declaration creates the following routes:

| Verb      | URI                    | Action  | Route name     |
| --------- | ---------------------- | ------- | -------------- |
| GET       | `/photos`              | index   | photos.index   |
| GET       | `/photos/create`       | create  | photos.create  |
| POST      | `/photos`              | store   | photos.store   |
| GET       | `/photos/{photo}`      | show    | photos.show    |
| GET       | `/photos/{photo}/edit` | edit    | photos.edit    |
| PUT/PATCH | `/photos/{photo}`      | update  | photos.update  |
| DELETE    | `/photos/{photo}`      | destroy | photos.destroy |

To register only a subset of these routes, use `only` or `except`:

```php theme={null}
Route::resource('photos', PhotoController::class)->only(['index', 'show']);

Route::resource('photos', PhotoController::class)->except(['create', 'store', 'update', 'destroy']);
```

## Listing defined routes

Display all registered routes with the Artisan command:

```shell theme={null}
php artisan route:list
```

Include middleware information:

```shell theme={null}
php artisan route:list -v
```

Filter routes by URI prefix:

```shell theme={null}
php artisan route:list --path=user
```

## Next steps

<Card title="Blade templates" icon="code" href="/en/blade">
  Learn how to use Laravel's Blade templating engine to build dynamic views.
</Card>


## Related topics

- [Request Lifecycle](/en/lifecycle.md)
- [Password Reset](/en/passwords.md)
- [Laravel 11+ New Application Structure FAQ](/en/advanced/app-structure-faq.md)
- [Queues](/en/queues.md)
- [March 2026 Laravel Updates](/en/blog/changelog/202603.md)
