Bird
Raised Fist0
Laravelframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which HTTP method in Laravel routing is typically used to retrieve data from the server without changing it?
easy
A. GET
B. POST
C. PUT
D. DELETE

Solution

  1. Step 1: Understand HTTP methods purpose

    GET requests are designed to fetch or read data without modifying it.
  2. Step 2: Match method to Laravel routing

    In Laravel, routes defined with GET respond to data retrieval requests.
  3. Final Answer:

    GET -> Option A
  4. Quick Check:

    GET = Read data [OK]
Hint: GET is for reading data, no changes made [OK]
Common Mistakes:
  • Confusing POST with GET for data retrieval
  • Using PUT or DELETE for fetching data
  • Thinking GET modifies data
2. Which of the following is the correct Laravel route definition for handling a POST request to '/submit'?
easy
A. Route::get('/submit', [FormController::class, 'store']);
B. Route::post('/submit', [FormController::class, 'store']);
C. Route::put('/submit', [FormController::class, 'store']);
D. Route::delete('/submit', [FormController::class, 'store']);

Solution

  1. Step 1: Identify the HTTP method for creating data

    POST is used to send data to the server, typically for creating resources.
  2. Step 2: Match Laravel route syntax for POST

    Route::post() defines a route that listens for POST requests.
  3. Final Answer:

    Route::post('/submit', [FormController::class, 'store']); -> Option B
  4. Quick Check:

    POST route syntax = Route::post('/submit', [FormController::class, 'store']); [OK]
Hint: Use Route::post() for form submissions [OK]
Common Mistakes:
  • Using Route::get() for POST requests
  • Mixing PUT or DELETE with form submission
  • Incorrect method name in route definition
3. Given the following Laravel routes, what HTTP method should you use to update a user's profile?
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']);
medium
A. PUT
B. GET
C. POST
D. DELETE

Solution

  1. Step 1: Identify route for updating data

    The route with Route::put() is used for updating existing resources.
  2. Step 2: Match HTTP method to update action

    PUT method is the standard for updating a resource like a user profile.
  3. Final Answer:

    PUT -> Option A
  4. Quick Check:

    Update = PUT method [OK]
Hint: PUT updates existing data, POST creates new [OK]
Common Mistakes:
  • Using POST instead of PUT for updates
  • Confusing GET with update actions
  • Using DELETE to update data
4. You wrote this Laravel route to delete a post:
Route::post('/post/{id}', [PostController::class, 'destroy']);

Why might this cause a problem when trying to delete a post?
medium
A. The URL should not include an {id} parameter
B. Route syntax is incorrect for any HTTP method
C. The controller method name 'destroy' is invalid
D. POST method is not intended for deleting resources

Solution

  1. Step 1: Understand HTTP methods for resource deletion

    DELETE method is the standard for deleting resources, not POST.
  2. Step 2: Identify mismatch in route method

    Using Route::post() for deletion can cause unexpected behavior or conflicts.
  3. Final Answer:

    POST method is not intended for deleting resources -> Option D
  4. Quick Check:

    DELETE method needed for deletion [OK]
Hint: Use Route::delete() for deleting resources [OK]
Common Mistakes:
  • Using POST instead of DELETE for deletions
  • Ignoring HTTP method conventions
  • Assuming controller method name causes error
5. You want to create a Laravel API that supports full CRUD operations on a 'product'. Which set of HTTP methods and routes correctly matches the standard RESTful pattern?
hard
A. Route::put('/product', [ProductController::class, 'index']);\nRoute::delete('/product', [ProductController::class, 'store']);\nRoute::get('/product/{id}', [ProductController::class, 'update']);\nRoute::post('/product/{id}', [ProductController::class, 'destroy']);
B. Route::post('/product', [ProductController::class, 'index']);\nRoute::get('/product', [ProductController::class, 'store']);\nRoute::delete('/product/{id}', [ProductController::class, 'update']);\nRoute::put('/product/{id}', [ProductController::class, 'destroy']);
C. 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']);
D. Route::delete('/product', [ProductController::class, 'index']);\nRoute::put('/product', [ProductController::class, 'store']);\nRoute::post('/product/{id}', [ProductController::class, 'update']);\nRoute::get('/product/{id}', [ProductController::class, 'destroy']);

Solution

  1. Step 1: Match HTTP methods to CRUD actions

    GET for reading list (index), POST for creating (store), PUT for updating, DELETE for deleting.
  2. 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.
  3. 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 C
  4. Quick 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]
Hint: Follow REST: GET-read, POST-create, PUT-update, DELETE-remove [OK]
Common Mistakes:
  • Mixing HTTP methods and controller actions
  • Incorrect URL patterns for resource IDs
  • Using wrong methods for CRUD operations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard