HTTP method routing helps your Laravel app know what to do when someone visits a URL or sends data. It tells the app how to handle different actions like showing a page, saving data, updating, or deleting.
HTTP method routing (GET, POST, PUT, DELETE) in Laravel
Route::get('/url', [Controller::class, 'method']); Route::post('/url', [Controller::class, 'method']); Route::put('/url', [Controller::class, 'method']); Route::delete('/url', [Controller::class, 'method']);
Each method matches a specific HTTP request type.
Use controllers to keep your code organized and clean.
Route::get('/users', [UserController::class, 'index']);
Route::post('/users', [UserController::class, 'store']);
Route::put('/users/{id}', [UserController::class, 'update']);
Route::delete('/users/{id}', [UserController::class, 'destroy']);
This example shows how to set up routes for all four HTTP methods in Laravel. The UserController handles showing users, adding a new user, updating a user, and deleting a user. Each method returns a JSON response.
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\UserController; Route::get('/users', [UserController::class, 'index']); Route::post('/users', [UserController::class, 'store']); Route::put('/users/{id}', [UserController::class, 'update']); Route::delete('/users/{id}', [UserController::class, 'destroy']); // UserController.php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { private static $users = [ 1 => ['name' => 'Alice'], 2 => ['name' => 'Bob'], ]; public function index() { return response()->json(self::$users); } public function store(Request $request) { $newId = count(self::$users) + 1; self::$users[$newId] = ['name' => $request->input('name')]; return response()->json(['message' => 'User added', 'user' => self::$users[$newId]], 201); } public function update(Request $request, $id) { if (!isset(self::$users[$id])) { return response()->json(['message' => 'User not found'], 404); } self::$users[$id]['name'] = $request->input('name'); return response()->json(['message' => 'User updated', 'user' => self::$users[$id]]); } public function destroy($id) { if (!isset(self::$users[$id])) { return response()->json(['message' => 'User not found'], 404); } unset(self::$users[$id]); return response()->json(['message' => 'User deleted']); } }
Laravel routes are defined in the routes/web.php or routes/api.php files.
Use Route::resource() for quick RESTful routing if you want all methods automatically.
Remember to test your routes with tools like Postman or your browser.
HTTP method routing tells Laravel how to respond to different types of requests.
GET is for reading data, POST for creating, PUT for updating, and DELETE for removing.
Use controllers to keep your routing logic clean and organized.
Practice
Solution
Step 1: Understand HTTP methods purpose
GET requests are designed to fetch or read data without modifying it.Step 2: Match method to Laravel routing
In Laravel, routes defined with GET respond to data retrieval requests.Final Answer:
GET -> Option AQuick Check:
GET = Read data [OK]
- Confusing POST with GET for data retrieval
- Using PUT or DELETE for fetching data
- Thinking GET modifies data
Solution
Step 1: Identify the HTTP method for creating data
POST is used to send data to the server, typically for creating resources.Step 2: Match Laravel route syntax for POST
Route::post() defines a route that listens for POST requests.Final Answer:
Route::post('/submit', [FormController::class, 'store']); -> Option BQuick Check:
POST route syntax = Route::post('/submit', [FormController::class, 'store']); [OK]
- Using Route::get() for POST requests
- Mixing PUT or DELETE with form submission
- Incorrect method name in route definition
Route::get('/user/{id}', [UserController::class, 'show']);
Route::post('/user', [UserController::class, 'store']);
Route::put('/user/{id}', [UserController::class, 'update']);
Route::delete('/user/{id}', [UserController::class, 'destroy']);Solution
Step 1: Identify route for updating data
The route with Route::put() is used for updating existing resources.Step 2: Match HTTP method to update action
PUT method is the standard for updating a resource like a user profile.Final Answer:
PUT -> Option AQuick Check:
Update = PUT method [OK]
- Using POST instead of PUT for updates
- Confusing GET with update actions
- Using DELETE to update data
Route::post('/post/{id}', [PostController::class, 'destroy']);Why might this cause a problem when trying to delete a post?
Solution
Step 1: Understand HTTP methods for resource deletion
DELETE method is the standard for deleting resources, not POST.Step 2: Identify mismatch in route method
Using Route::post() for deletion can cause unexpected behavior or conflicts.Final Answer:
POST method is not intended for deleting resources -> Option DQuick Check:
DELETE method needed for deletion [OK]
- Using POST instead of DELETE for deletions
- Ignoring HTTP method conventions
- Assuming controller method name causes error
Solution
Step 1: Match HTTP methods to CRUD actions
GET for reading list (index), POST for creating (store), PUT for updating, DELETE for deleting.Step 2: Verify route patterns and controller methods
Routes in Route::get('/product', [ProductController::class, 'index']);\nRoute::post('/product', [ProductController::class, 'store']);\nRoute::put('/product/{id}', [ProductController::class, 'update']);\nRoute::delete('/product/{id}', [ProductController::class, 'destroy']); correctly pair HTTP methods with controller actions and URL patterns.Final Answer:
Route::get('/product', [ProductController::class, 'index']);\nRoute::post('/product', [ProductController::class, 'store']);\nRoute::put('/product/{id}', [ProductController::class, 'update']);\nRoute::delete('/product/{id}', [ProductController::class, 'destroy']); -> Option CQuick Check:
GET, POST, PUT, DELETE with correct methods = Route::get('/product', [ProductController::class, 'index']);\nRoute::post('/product', [ProductController::class, 'store']);\nRoute::put('/product/{id}', [ProductController::class, 'update']);\nRoute::delete('/product/{id}', [ProductController::class, 'destroy']); [OK]
- Mixing HTTP methods and controller actions
- Incorrect URL patterns for resource IDs
- Using wrong methods for CRUD operations
Ready to prove it?
Pass the test and this pattern moves unknown
