Mastering Laravel Routing – Part 2

aravel Laravel Views & Blade Templates – Beginner’s Guide

Available Router Methods in Laravel

Laravel provides several methods to define routes that respond to different HTTP verbs. Each verb has a specific purpose in web development.


Basic Route Methods

You can define routes for standard HTTP verbs using:

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

Examples:

// GET → Show a page
Route::get('/about', fn() => 'About Page');

// POST → Handle form submission
Route::post('/contact', fn() => 'Form submitted!');

// PUT → Replace entire resource
Route::put('/profile', fn() => 'Profile replaced.');

// PATCH → Partially update resource
Route::patch('/profile', fn() => 'Profile partially updated.');

// DELETE → Remove a resource
Route::delete('/user', fn() => 'User deleted.');

// OPTIONS → Tell client what methods are allowed
Route::options('/user', fn() => response('', 200)
    ->header('Allow', 'GET, POST, PUT, PATCH, DELETE'));

Special Purpose of PATCH and OPTIONS

PATCH

  • Used to partially update a resource.
  • Unlike PUT (which usually replaces everything), PATCH changes only the fields sent.

Example: Updating just the name in a profile:

PATCH /user/1
{
  "name": "Haroon Rasheed"
}

OPTIONS

  • Used by browsers or clients to ask the server: “Which HTTP methods are allowed for this route?”
  • Often used in CORS preflight requests when calling APIs from another domain.

Example response:

Allow: GET, POST, PUT, PATCH, DELETE

Handling Multiple Verbs

Sometimes you want a route to handle multiple verbs.

Using match()

Route::match(['get', 'post'], '/subscribe', function () {
    return 'Subscribe via GET or POST';
});

Using any()

Route::any('/test', function () {
    return 'This route accepts ANY HTTP verb!';
});

Route Order Matters

When defining routes with the same URI:

  • Always define specific verbs (get, post, etc.) first,
  • Then use generic routes (any, match) later.

Correct order:

Route::get('/home', fn() => 'GET Home');
Route::any('/home', fn() => 'Any Home');

Summary Table

HTTP VerbPurposeLaravel Example
GETRetrieve data (read)Route::get('/users', ...)
POSTCreate new dataRoute::post('/users', ...)
PUTReplace entire resourceRoute::put('/user/1', ...)
PATCHPartially update a resourceRoute::patch('/user/1', ...)
DELETERemove a resourceRoute::delete('/user/1', ...)
OPTIONSCheck which methods are allowed (CORS, preflight)Route::options('/users', ...)