Laravel HTTP Methods and Routes
An HTTP method is the kind of request a browser sends. The method says what the visitor wants to do, and the route answers only its own method.
Read with GET
This route answers a GET request for the notes.
Route::get('/l09/notes', fn () => 'Here are the notes.');Save with POST
This route answers a POST request for the notes.
Route::post('/l09/notes', fn () => 'Note saved.');Change with PUT
This route answers a PUT request for note 5.
Route::put('/l09/notes/{id}', fn (string $id) => "Note {$id} updated.");Change part with PATCH
This route answers a PATCH request for note 5.
Route::patch('/l09/notes/{id}', fn (string $id) => "Note {$id} patched.");Remove with DELETE
This route answers a DELETE request for note 5.
Route::delete('/l09/notes/{id}', fn (string $id) => "Note {$id} deleted.");Example
<?php
use Illuminate\Support\Facades\Route;
Route::get('/l09/notes', fn () => 'Here are the notes.');
Route::post('/l09/notes', fn () => 'Note saved.');Key points
- HTTP method: the kind of request a browser sends.
- GET reads a page, and POST saves new data.
- PUT replaces a record, and PATCH changes part of a record.
- DELETE removes a record.
- One address can hold a different route for each method.
In this example, the first lines load the Route helper. The next line adds a GET route that reads the notes. The last line adds a POST route that saves a note.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which HTTP method saves new data?