agyanadda@gmail.com

Laravel Routing

Laravel Basics 1 min read Updated Dec 2, 2026 Topic 4 of 31

A route connects a URL and HTTP method to the code that should run. Web routes live in routes/web.php.

Basic routes

  • Route::get('/about', ...) — show a page
  • Route::post('/contact', ...) — receive a form
  • Route::put, Route::patch, Route::delete — update and delete
  • Route::view('/terms', 'terms') — return a view directly

Route parameters

Put a parameter in curly braces: /users/{id}. Laravel passes it to your function. Add ? for an optional parameter: /hello/{name?} — and give it a default value.

Named routes

Give a route a name with ->name('posts.index'), then build its URL anywhere with route('posts.index'). If the URL changes later, every link still works.

See all routes

Run php artisan route:list to see every route, its method, name and controller.

Example

Example — Parameters and named routes PHP
<?php
use Illuminate\Support\Facades\Route;

Route::get('/users/{id}', function (int $id) {
    return "User number $id";
})->name('users.show');

Route::get('/hello/{name?}', function (string $name = 'Guest') {
    return "Hello, $name";
});

// Building a URL from the name:
// route('users.show', 5)  =>  http://127.0.0.1:8000/users/5

/users/5 shows "User number 5". /hello shows "Hello, Guest" and /hello/Asha shows "Hello, Asha".

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which method defines a route for submitting a form?

Exercise 2 True / False

Using route('name') keeps links working even if the URL path changes.

Exercise 3 Fill in the Blank

Run php artisan route:___ to see all routes.

Frequently Asked Questions

web.php routes use sessions, cookies and CSRF protection for browser pages. api.php routes are stateless, get the /api prefix and usually return JSON. In Laravel 11 and later, api.php is added with php artisan install:api.