Laravel Routing

Print Print
Reading time 1:9

Routing in Laravel maps incoming HTTP requests to the code that should handle them. Routes are typically defined in the routes/web.php and routes/api.php files and can point to a closure or a controller method.

Basic Routes

Defining routes

Route::get('/posts', [PostController::class, 'index']);
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts/{id}', [PostController::class, 'show']);

Laravel provides route methods matching each of the common HTTP verbs, including get, post, put, patch, and delete.

Route Parameters

Segments wrapped in curly braces, such as {id} above, are captured and passed as arguments to the route's closure or controller method. Parameters can be made optional with a trailing ? and constrained to a pattern with the where method.

Named Routes and Groups

Routes can be given a name, allowing URLs or redirects to be generated without hardcoding the actual path. Related routes can also be organized into groups that share attributes such as a URL prefix, middleware, or a controller namespace.

Route group example

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index']);
    Route::get('/settings', [SettingsController::class, 'index']);
});

Resource Routing

For controllers that handle typical create, read, update, and delete operations, Route::resource registers all of the corresponding routes in a single line, following Laravel's RESTful naming conventions.

By: Tomas Silny
Edited: 2026-08-13 03:31:20