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.
The shortcut
Read any route line as a sentence: "When a METHOD arrives at PATH, return THIS." Two routes can share a path as long as the methods differ.
Show the list
"When a GET arrives at /books, return the list view."
Route::get('/books', ...).
The trap is expecting a form to reach a Route::get route. If the methods do not match, Laravel answers 405 Method Not Allowed.
Drills
Answer fast — trust the rule.
Which route answers a browser visiting /about?
What does a route that returns ['title' => 'Dune'] send back?
A form posts to /books but only Route::get('/books', ...) exists. What happens?
A POST form in web.php returns error 419. What is most likely missing?
Remember
- Method plus path picks the code; the return value becomes the response.
- Read a route as: "When a METHOD arrives at PATH, return THIS."
- Trap: a mismatched method gives 405, and a missing
@csrfgives 419.
