Your first route

One GET route

Routes live in the routes/web.php file. Each route pairs a URL with the code that answers it, so the file reads like a map of the site.

The simplest route answers one address with a short string. Add it, save the file, and open the address in the browser.

use Illuminate\Support\Facades\Route; Route::get("/hello", function () { return "Hello from Laravel!"; });

Closure routes

A route that uses an anonymous function is called a closure route. Closures are perfect for tiny pages, prototypes, and homework tasks.

POST routes work the same way. They usually answer forms and return JSON instead of a page.

Route::post("/contact", function () { return response()->json(["status" => "saved"]); });

Return a view

Real pages live in Blade view files, not in strings. The view helper renders a template by name and passes data to it as an array.

The template below receives a name variable. You will print it with Blade syntax in the next lessons.

Route::get("/about", function () { return view("about", ["name" => "Ada"]); });

Route parameters

Curly braces mark the variable parts of a URL. Laravel passes each part into the closure as an argument with the same name.

Type the argument to document what the route expects. A string type is enough for the first user pages.

Route::get("/user/{id}", function (string $id) { return "User " . $id; });

Next: Controllers

Article author: Arthur Isaev