Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Routing  ›  Lesson

Basic Routes

Basic Routes 12 minA route pairs an HTTP method and a URI with the code to run, and whatever that code returns becomes the response.

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 record

Two 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.

Lendly's book list and its formStep 1 of 4
  1. 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.

quick drill

Which route answers a browser visiting /about?

quick drill

What does a route that returns ['title' => 'Dune'] send back?

quick drill

A form posts to /books but only Route::get('/books', ...) exists. What happens?

quick drill

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 @csrf gives 419.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainMethod plus path

Lendly needs a page listing books and a form that submits a new book to the same /books address. Write both routes and explain how Laravel tells them apart and what each returns.