0
0
Laravelframework~10 mins

HTTP method routing (GET, POST, PUT, DELETE) in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HTTP method routing (GET, POST, PUT, DELETE)
Client sends HTTP request
Laravel receives request
Check HTTP method: GET, POST, PUT, DELETE
GET
Route to GET handler
Route to POST handler
Route to PUT handler
Route to DELETE handler
Controller method runs
Response sent back to client
Laravel routes HTTP requests by checking the method (GET, POST, PUT, DELETE) and sending the request to the matching handler.
Execution Sample
Laravel
Route::get('/items', fn() => 'List items');
Route::post('/items', fn() => 'Create item');
Route::put('/items/{id}', fn($id) => "Update item $id");
Route::delete('/items/{id}', fn($id) => "Delete item $id");
Defines routes for GET, POST, PUT, DELETE methods on /items URL with simple responses.
Execution Table
StepHTTP MethodURLRoute MatchedHandler Output
1GET/itemsRoute::get('/items')List items
2POST/itemsRoute::post('/items')Create item
3PUT/items/5Route::put('/items/{id}')Update item 5
4DELETE/items/5Route::delete('/items/{id}')Delete item 5
5PATCH/items/5No route matched404 Not Found
💡 Step 5 stops because PATCH method has no matching route.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
HTTP MethodNoneGETPOSTPUTDELETEPATCH
URLNone/items/items/items/5/items/5/items/5
Route MatchedNoneGET /itemsPOST /itemsPUT /items/{id}DELETE /items/{id}None
Handler OutputNoneList itemsCreate itemUpdate item 5Delete item 5404 Not Found
Key Moments - 3 Insights
Why does the PATCH request not match any route?
Because no route is defined for the PATCH HTTP method in the execution_table row 5, Laravel returns 404 Not Found.
How does Laravel know which controller method to run?
Laravel matches the HTTP method and URL pattern to the route definition, then runs the handler defined for that route as shown in execution_table rows 1-4.
What happens if the URL matches but the HTTP method does not?
Laravel does not match the route and returns 404 Not Found, as seen in step 5 where URL matches /items/5 but method PATCH is not defined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the handler output at step 3?
ACreate item
BUpdate item 5
CDelete item 5
DList items
💡 Hint
Check the row where HTTP Method is PUT and URL is /items/5.
At which step does the HTTP method become PATCH and no route matches?
AStep 2
BStep 3
CStep 5
DStep 4
💡 Hint
Look for the step with HTTP Method PATCH and Route Matched as None.
If we add Route::patch('/items/{id}', fn($id) => "Patch item $id"), what would be the handler output at step 5?
A404 Not Found
BPatch item 5
CDelete item 5
DUpdate item 5
💡 Hint
Adding a PATCH route matching /items/{id} changes the output for PATCH requests.
Concept Snapshot
Laravel HTTP method routing:
- Use Route::get(), Route::post(), Route::put(), Route::delete() for different HTTP methods.
- Laravel matches request method and URL to route.
- Runs the matched route's handler.
- No match returns 404 Not Found.
- Define routes for all methods you want to support.
Full Transcript
In Laravel, HTTP method routing works by matching the incoming request's HTTP method and URL to the defined routes. For example, Route::get('/items') handles GET requests to /items, Route::post('/items') handles POST requests, and so on. When a request comes in, Laravel checks the method: if it is GET, it looks for a GET route matching the URL; if POST, it looks for a POST route, etc. If a matching route is found, Laravel runs the handler function or controller method defined for that route and sends the response back. If no route matches the method and URL, Laravel returns a 404 Not Found error. This way, you can define different behaviors for the same URL depending on the HTTP method, like listing items with GET, creating items with POST, updating with PUT, and deleting with DELETE.