Basic Routes
The first thing Lendly must do is answer when someone visits a URL. A route is that answer: "when this kind of request arrives at this address, run this code." Every page, form and API endpoint in Laravel starts as one line in a routes file.
The rule
Method plus path picks the code; the return value becomes the response.
A route has three parts: the HTTP method, the URI and the action:
use Illuminate\Support\Facades\Route;
Route::get('/books', function () {
return 'All the books';
});The router has a method for each HTTP verb:
Route::get($uri, $action); // read a page
Route::post($uri, $action); // submit a form
Route::put($uri, $action); // replace a record
Route::patch($uri, $action); // update part of a record
Route::delete($uri, $action); // remove a recordTwo extras cover several verbs at once: Route::match(['get', 'post'], '/search', ...) for a chosen list, and Route::any(...) for every verb.
What you return decides what is sent back:
- a string → an HTML response with that text
view('books.index')→ a rendered Blade page- an array or a model → JSON, automatically
Route::get('/stats', function () {
return ['books' => 128, 'loans' => 37]; // sent as JSON
});Browser routes live in routes/web.php. That file's routes get sessions and CSRF protection, so any POST, PUT, PATCH or DELETE form must include a @csrf token, or Laravel rejects it with a 419 error.
To see every route the app knows, run php artisan route:list.