GitHub

Routing

Basic Routing

Define routes in routes/web.php using URI and closure:

Route::get('foo', function () {
    return response('Hello World');
});

Route::post('foo', function () {
    // Handle POST request
});

Available Router Methods

Register routes for any HTTP verb:

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::any($uri, $callback);
Route::options($uri, $callback);
Route::view($uri, $callback);

Route Parameters

Required Parameters

Route::get('user/{id}', function ($id) {
    return response('User '.$id);
});

Multiple parameters example:

Route::get('posts/{postId}/comments/{commentId}', function ($postId, $commentId) {
    // Logic here
});

Named Routes

Route::get('/profile', $callback)->name('profile');

Generating URLs To Named Routes

// Basic route URL
$url = route_url('profile');

// Parameterized route URL
$url = route_url('post.comment', ['postId' => 1, 'commentId' => 2]);

Route Groups

Group routes with shared attributes:

Route::group(['middleware' => ['csrf'], 'path' => '/admin'], function($router) {
    $router->get('/dashboard', [AdminController::class, 'dashboard']);
    // More routes...
});

Available Grouping Route Attributes

  • middleware - Array of middlewares
  • path - Group path prefix
  • method - Array of HTTP methods
  • name - Route name prefix
  • callback - Controller class
  • template - View template
Tip: Route groups help organize related routes and apply shared settings efficiently.