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:Default route files
All Laravel routes are defined in the files inside theroutes/ directory. Laravel automatically loads these files based on the configuration in bootstrap/app.php.
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:
API Routes
If your application will also offer a stateless API, you may enable API routing using theinstall:api Artisan command:
install:api command installs Laravel 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:
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. 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:
Available HTTP methods
The router can register routes that respond to any HTTP verb:match or any:
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.Returning a view
When a route only needs to return a view, useRoute::view for a concise definition:
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:
Optional parameters
Make a parameter optional by adding? after its name and providing a default value for the corresponding argument:
Regular expression constraints
Constrain the format of a route parameter using thewhere method:
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:Generating URLs to named routes
Use theroute helper to generate a URL from a route name:
Redirecting to named routes
Route groups
Group related routes to share middleware, controllers, prefixes, and other attributes without repeating them on each route.Middleware
Controllers
Group routes that share the same controller:Prefixes
Add a common URI prefix to a group of routes:Name prefixes
Prefix the name of every route in a group:Resource routes
For controllers that handle CRUD operations on a resource,Route::resource registers all the standard routes in one line:
| 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 |
only or except:
Listing defined routes
Display all registered routes with the Artisan command:Next steps
Blade templates
Learn how to use Laravel’s Blade templating engine to build dynamic views.