A Laravel route is the rule that links an HTTP request to the code that should handle it.
In classic PHP (without frameworks), the URL used to match the file. If you visited /contact.php, the server looked for a file named contact.php.
In Laravel, that is over. The URL is independent of the file. You decide which URL triggers which code. This is done through the Route System, and the HTML is managed with a super powerful engine called Blade.
Routes (routes/web.php)
The gatekeeper of your application is the file routes/web.php. Here you define all the URLs for your website.
Basic Syntax:
Route::[verb]('url', [function]);
use Illuminate\Support\Facades\Route;
// 1. A simple route that returns text
Route::get('/hello', function () {
return 'Hello world from Laravel!';
});
// 2. A route with parameters (e.g., /product/5)
Route::get('/product/{id}', function ($id) {
return "You are viewing product: " . $id;
});If you now go to your-site.test/hello, you will see the message. Without creating any new .php file in the public folder!
Views and Blade
Obviously, you won’t write all your HTML inside that function in the routes. That’s what Views are for. They are stored in resources/views.
Laravel uses Blade, a template engine that makes writing HTML a pleasure. The files must end in .blade.php.
Why is Blade better than pure PHP? Look at the difference for printing a variable and escaping XSS (security):
- Pure PHP:
Hello, <?= htmlspecialchars($name) ?> - Blade:
Hello, {{ $name }}
Blade does htmlspecialchars automatically for you. It’s cleaner and more secure.
From Route to View
Let’s connect both worlds.
Step 1: Create the view (resources/views/home.blade.php)
<h1>Welcome, {{ $name }}</h1>
<p>Today is {{ date('d/m/Y') }}</p>Step 2: Return the view from the route (routes/web.php)
Route::get('/', function () {
// The view() function looks for the file in resources/views
// The second parameter is an array with the data we pass to the view
return view('home', ['name' => 'Carlos']);
});Blade Directives
Blade eliminates the need to constantly open and close <?php ... ?> tags. It uses directives that start with @.
Conditionals (if):
@if ($age >= 18)
<p>You are of legal age</p>
@else
<p>Access denied</p>
@endifLoops (foreach):
<ul>
@foreach ($products as $product)
<li>{{ $product->name }} - {{ $product->price }}€</li>
@endforeach
</ul>Layouts: The end of require 'header.php'
In Module 8 (and in classic PHP), to avoid repeating the menu and footer on every page, you would do require 'header.php'.
Blade turns the tables with Template Inheritance. You create a master template (“Layout”) with the empty slots, and the child pages fill those slots.
The Master Layout (resources/views/layout.blade.php):
<html>
<body>
<nav>My Fixed Menu</nav>
<div class="content">
@yield('content')
</div>
<footer>My Fixed Footer</footer>
</body>
</html>The Child Page (resources/views/profile.blade.php):
@extends('layout')
@section('content')
<h1>My Profile</h1>
<p>This is the unique content of this page.</p>
@endsectionResult: Laravel combines both files automatically. If you change the menu in the layout, it changes across the entire website.