Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/MonishAMPT/fastroute-code/llms.txt

Use this file to discover all available pages before exploring further.

Web routes handle requests that return HTML content, such as web pages, forms, and traditional server-rendered views.

Basic Web Route Definition

Web routes are defined in routes/web.php and return a function that accepts a RouteCollector instance:
routes/web.php
<?php

use FastRoute\RouteCollector;

return function (RouteCollector $r) {
    // Your web routes go here
};

Creating Web Routes

Simple Page Routes

Define routes that render HTML views:
routes/web.php
$r->get('/', function() {
    require __DIR__ . '/../views/main.php';
});

$r->get('/about', function() {
    require __DIR__ . '/../views/about.php';
});
Web routes typically use inline closures that include or require view files from the views/ directory.

Organizing Views

Store your HTML templates in a dedicated views/ directory:
project-root/
├── routes/
│   └── web.php
├── views/
│   ├── main.php
│   ├── about.php
│   ├── contact.php
│   └── layouts/
│       └── app.php

Example View File

views/main.php
<!DOCTYPE html>
<html>
<head>
    <title>Home Page</title>
</head>
<body>
    <h1>Welcome to My Site</h1>
    <p>This is the home page.</p>
</body>
</html>

Content-Type Headers

FastRoute automatically sets the correct content type based on the route path:
index.php
if (strpos($uri, '/api/') === 0) {
    header('Content-Type: application/json; charset=utf-8');
} else {
    header('Content-Type: text/html; charset=utf-8');
}
All routes that don’t start with /api/ automatically get HTML content type headers.

Dynamic Routes with Parameters

You can create dynamic web routes with parameters:
routes/web.php
$r->get('/blog/{slug}', function($vars) {
    $slug = $vars['slug'];
    require __DIR__ . '/../views/blog.php';
});

$r->get('/user/{id:\d+}', function($vars) {
    $userId = $vars['id'];
    require __DIR__ . '/../views/user.php';
});
The view file can access the parameters:
views/blog.php
<!DOCTYPE html>
<html>
<body>
    <h1>Blog Post: <?php echo htmlspecialchars($slug); ?></h1>
</body>
</html>

Handling Forms

Create routes for displaying and processing forms:
routes/web.php
// Display form
$r->get('/contact', function() {
    require __DIR__ . '/../views/contact-form.php';
});

// Process form submission
$r->post('/contact', function() {
    // Process POST data
    $name = $_POST['name'] ?? '';
    $email = $_POST['email'] ?? '';
    
    // Handle form submission
    // ...
    
    // Redirect or show success
    require __DIR__ . '/../views/contact-success.php';
});

Route Groups for Web Pages

Organize related web pages using route groups:
routes/web.php
// Public pages
$r->addGroup('', function (FastRoute\RouteCollector $r) {
    $r->get('/', function() {
        require __DIR__ . '/../views/main.php';
    });
    
    $r->get('/about', function() {
        require __DIR__ . '/../views/about.php';
    });
});

// Admin pages
$r->addGroup('/admin', function (FastRoute\RouteCollector $r) {
    $r->get('/dashboard', function() {
        require __DIR__ . '/../views/admin/dashboard.php';
    });
    
    $r->get('/users', function() {
        require __DIR__ . '/../views/admin/users.php';
    });
});

Complete Example

Here’s the complete web routes file from the source code:
routes/web.php
<?php

use FastRoute\RouteCollector;

return function (RouteCollector $r) {

    $r->get('/', function() {
        require __DIR__ . '/../views/main.php';
    });

    $r->get('/about', function() {
        require __DIR__ . '/../views/about.php';
    });
};

Best Practices

Use closures in web routes primarily for loading views. Move complex logic to controllers or handler classes.
Group related views in subdirectories like views/blog/, views/admin/, etc.
Create reusable layout files to avoid duplicating HTML structure across views.
Always use htmlspecialchars() when outputting user data in views to prevent XSS attacks.

Differences from API Routes

Content Type

Web routes return HTML (text/html), while API routes return JSON (application/json)

Response Format

Web routes use require to include views, API routes use echo json_encode()

File Organization

Web routes load from views/, API routes typically call handlers or return data

Use Case

Web routes for browsers and HTML pages, API routes for AJAX, mobile apps, and integrations

Next Steps

Route Handlers

Learn about different handler types

API Routes

Create API endpoints that return JSON

Build docs developers (and LLMs) love