================ CODE SNIPPETS ================ TITLE: Assign Middleware with Single Parameter to Route DESCRIPTION: Shows how to apply middleware with a single parameter to a specific route definition. The middleware and its parameter are specified using the `middleware` method, separated by a colon. SOURCE: https://laravel.com/docs/12.x/middleware LANGUAGE: php CODE: ``` use App\Http\Middleware\EnsureUserHasRole; Route::put('/post/{id}', function (string $id) { // ... })->middleware(EnsureUserHasRole::class.':editor'); ``` -------------------------------- TITLE: Assign Middleware Groups to Laravel Routes DESCRIPTION: This code illustrates how to apply middleware groups to routes in Laravel. Middleware groups can be assigned directly to a route using the `middleware` method, or a group of routes can be protected by a middleware group using the `group` method. SOURCE: https://laravel.com/docs/12.x/middleware LANGUAGE: php CODE: ``` Route::get('/', function () { // ... })->middleware('group-name'); Route::middleware(['group-name'])->group(function () { // ... }); ``` -------------------------------- TITLE: Replace Middleware in Default Laravel Groups DESCRIPTION: This snippet demonstrates how to replace an existing middleware within Laravel's default `web` or `api` middleware groups with a custom middleware. It uses the `replace` option within the `web()` or `api()` methods. SOURCE: https://laravel.com/docs/12.x/middleware LANGUAGE: php CODE: ``` use App\Http\Middleware\StartCustomSession; use Illuminate\Session\Middleware\StartSession; $middleware->web(replace: [ StartSession::class => StartCustomSession::class, ]); ``` -------------------------------- TITLE: Assign Single Middleware to a Route (PHP) DESCRIPTION: This code demonstrates how to assign a single middleware, `EnsureTokenIsValid`, to a specific route (`/profile`) using the `middleware` method in Laravel. This allows for targeted application of middleware logic to individual routes. SOURCE: https://laravel.com/docs/12.x/middleware LANGUAGE: php CODE: ``` use App\Http\Middleware\EnsureTokenIsValid; Route::get('/profile', function () { // ... })->middleware(EnsureTokenIsValid::class); ``` -------------------------------- TITLE: Apply Custom Middleware Alias to a Route in Laravel DESCRIPTION: Use the defined middleware alias ('subscribed' in this example) when assigning middleware to a specific route. SOURCE: https://laravel.com/docs/12.x/middleware LANGUAGE: php CODE: ``` Route::get('/profile', function () { // ... })->middleware('subscribed'); ``` -------------------------------- TITLE: Assign Multiple Middleware to a Route (PHP) DESCRIPTION: This example shows how to apply multiple middleware, `First` and `Second`, to a single route (`/`) by passing an array of middleware class names to the `middleware` method in Laravel. This is useful for applying a stack of security or processing checks to specific routes. SOURCE: https://laravel.com/docs/12.x/middleware LANGUAGE: php CODE: ``` Route::get('/', function () { // ... })->middleware([First::class, Second::class]); ``` -------------------------------- TITLE: Middleware to Perform Actions After Request Handling DESCRIPTION: A middleware example showing how to execute code after the request has been handled by the application, allowing modification of the response. SOURCE: https://laravel.com/docs/12.x/middleware LANGUAGE: php CODE: ``` middleware(EnsureUserHasRole::class.':editor,publisher'); ``` -------------------------------- TITLE: Middleware to Perform Actions Before Request Handling DESCRIPTION: A middleware example demonstrating how to execute code before the request is passed to the next handler in the application pipeline. SOURCE: https://laravel.com/docs/12.x/middleware LANGUAGE: php CODE: ``` app->singleton(TerminatingMiddleware::class); } ```