0PricingLogin
PHP Academy · Lesson

Protecting Routes with Middleware

Use auth and guest middleware to secure routes and redirect users.

What is Middleware?

Middleware is code that runs before or after an HTTP request reaches the controller. It is used for authentication, logging, CORS, rate limiting, etc.

Built-in auth Middleware

The auth middleware redirects unauthenticated users to the login page (or returns 401 for API requests).

<?php
Route::get("/dashboard", [DashboardController::class, "index"])
    ->middleware("auth");

All lessons in this course

  1. Authentication with Laravel Breeze
  2. Protecting Routes with Middleware
  3. Gates: Simple Authorization Checks
  4. Policies: Model-Based Authorization
← Back to PHP Academy