Your first route

Contents
One GET route
Closure routes
Return a view
Route parameters

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

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: