SyntaxStudy
Sign Up
Laravel Beginner 10 min read

Laravel Routing

Laravel routing is defined in routes/web.php (for web routes) and routes/api.php (for API routes). Routes map URLs to controller methods or closures.

Example
// routes/web.php
use App\Http\Controllers\PostController;

// Basic routes
Route::get('/', fn() => view('welcome'));
Route::get('/about', fn() => 'About page');

// Resource controller (creates 7 CRUD routes automatically)
Route::resource('posts', PostController::class);

// Route parameters
Route::get('/users/{id}', function (int $id) {
    return User::findOrFail($id);
});

// Named routes
Route::get('/profile', [ProfileController::class, 'show'])->name('profile');
// Use: route('profile') to generate URL

// Route groups
Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index']);
    Route::resource('posts', PostController::class);
});