> ## 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

> Learn the basics of defining routes, route parameters, and named routes in Laravel.

## Basic routing

The most basic Laravel routes accept a URI and a closure.
They let you easily define routes and their behavior without complex configuration files.

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

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

### The default route files

All Laravel routes are defined in route files in the `routes/` directory.
These files are automatically loaded by Laravel based on the configuration in `bootstrap/app.php`.

```mermaid theme={null}
flowchart LR
    A["Incoming request"] --> B{{"Request type"}}
    B -->|"For browsers<br>Session / CSRF"| C["routes/web.php<br>web middleware group"]
    B -->|"For APIs<br>Stateless"| D["routes/api.php<br>api middleware group"]
    B -->|"CLI commands"| 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 are wrapped in the `web` middleware group, which provides session, 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 offer a stateless API, you can enable API routing with the `install:api` Artisan command.

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

The `install:api` command installs [Laravel Sanctum](/en/sanctum) and creates a `routes/api.php` file.
Sanctum provides a simple, robust API token authentication guard for third-party API consumers, SPAs, and mobile applications.

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

Routes in `routes/api.php` are stateless and the `api` [middleware group](/en/middleware) is applied.
The `/api` URI prefix is also automatically applied to every route, so you don't need to add it manually.
To change the prefix, edit your application's `bootstrap/app.php` file.

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

### Available HTTP methods

The router allows you to register routes for 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);
```

Use `match` or `any` for a route that responds to multiple HTTP verbs.

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

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

<Info>
  Any HTML form that accepts `POST`, `PUT`, `PATCH`, or `DELETE` in a `web` route file must include the `@csrf` directive.
  Otherwise, the request will be rejected.
</Info>

## Routes that return views

If a route only needs to return a view, you can write it concisely with `Route::view`.

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

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

## Route parameters

### Required parameters

To capture a dynamic part of the URL, use route parameters.
Wrap the parameter in `{}` and receive it as an argument on the route callback.

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

You can also define multiple parameters.

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

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

### Optional parameters

To make a parameter optional, append `?` to the parameter name and give the corresponding argument a default value.

```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

You can constrain the format of parameters with regular expressions 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]+');
```

For common patterns, there are convenience methods you can use instead.

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

## Named routes

Named routes let you refer to a route by name when generating URLs or redirects.

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

### Generating URLs to named routes

To generate a URL from a route name, use the `route` helper.

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

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

### Redirecting

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

## Route groups

You can group related routes to share middleware, controllers, prefixes, and more.

```mermaid theme={null}
flowchart TD
    A["HTTP request"] --> B["Router"]
    B --> C{{"Matching route?"}}
    C -->|"No"| D["404 response"]
    C -->|"Yes"| E["Check route middleware"]
    E --> F{{"Does middleware<br>pass?"}}
    F -->|"Denied"| G["Redirect /<br>error response"]
    F -->|"Pass"| H["Execute controller /<br>closure"]
    H --> I["Generate response"]
    I --> J["Send 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

You can group routes that use 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

You can apply a common prefix to a set of route URIs.

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

## Listing routes

To list every route that has been defined, use the Artisan command.

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

Include middleware information as follows:

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

To display only routes that start with a specific URI:

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

## Next steps

<Card title="Views" icon="eye" href="/en/views">
  Learn how to create views and pass data to them from controllers.
</Card>


## Related topics

- [Laravel Socialite (Social Authentication)](/en/socialite.md)
- [Request lifecycle](/en/lifecycle.md)
- [March 2026 Laravel updates](/en/blog/changelog/202603.md)
- [Laravel 11+ New Application Structure FAQ](/en/advanced/app-structure-faq.md)
- [Queues](/en/queues.md)
