Member-only story
Laravel 12 Routing Features: A Comprehensive Guide with Examples
Looking to enhance your Laravel applications with the powerful routing features in Laravel 12? This guide will walk you through the most important routing capabilities with practical examples that you can implement right away.
1. Basic Route Definitions
Laravel’s routing system is simple yet powerful. Here’s how to define basic routes:
// routes/web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return 'About Us Page';
});
2. API Routes
You may enable API routing using the install:api
Artisan command:
php artisan install:api
The 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:
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');