HTTP method routing (GET, POST, PUT, DELETE) in Laravel
📖 Scenario: You are building a simple Laravel web application to manage a list of books. You want to set up routes that respond to different HTTP methods to handle viewing, adding, updating, and deleting books.
🎯 Goal: Create Laravel routes that handle GET, POST, PUT, and DELETE HTTP methods for a /books URL path.
📋 What You'll Learn
Create a route for GET /books that returns a list of books.
Create a route for POST /books that adds a new book.
Create a route for PUT /books/{id} that updates a book by its ID.
Create a route for DELETE /books/{id} that deletes a book by its ID.
💡 Why This Matters
🌍 Real World
Web applications often need to handle different HTTP methods to perform actions like viewing, creating, updating, and deleting data.
💼 Career
Understanding HTTP method routing is essential for backend developers working with Laravel or similar frameworks to build RESTful APIs and web apps.
Progress0 / 4 steps
1
Create a GET route for listing books
In the routes/web.php file, create a GET route for the path /books that returns the string 'List of books'.
Laravel
Hint
Use Route::get('/books', function () { ... }); to define the GET route.
2
Add a POST route for adding a new book
Below the GET route, add a POST route for the path /books that returns the string 'Add a new book'.
Laravel
Hint
Use Route::post('/books', function () { ... }); to define the POST route.
3
Add a PUT route for updating a book by ID
Add a PUT route for the path /books/{id} that returns the string 'Update book with id: ' . $id. Use the variable $id in the route closure parameter.
Laravel
Hint
Use Route::put('/books/{id}', function ($id) { ... }); to define the PUT route with a parameter.
4
Add a DELETE route for deleting a book by ID
Add a DELETE route for the path /books/{id} that returns the string 'Delete book with id: ' . $id. Use the variable $id in the route closure parameter.
Laravel
Hint
Use Route::delete('/books/{id}', function ($id) { ... }); to define the DELETE route with a parameter.
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
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 A
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
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 B
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?
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
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 D
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
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.