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.

A well-organized project structure makes it easier to maintain and scale your FastRoute application. This guide explains the recommended directory layout and the purpose of each component.

Directory Structure

Here’s the recommended structure for a FastRoute application:
project-root/
├── index.php              # Main entry point and dispatcher
├── routes/
│   ├── api.php           # API route definitions
│   └── web.php           # Web route definitions
├── v1/
│   └── test.php          # Class-based handlers
├── views/
│   ├── main.php          # View templates
│   └── about.php
├── vendor/                # Composer dependencies
└── composer.json          # Project dependencies

Core Files

index.php - Main Entry Point

The index.php file is the main entry point of your application. It handles:
  • Autoloading dependencies
  • Request parsing
  • Route dispatching
  • Response handling
index.php
<?php

// Autoload Composer dependencies
require 'vendor/autoload.php';

use FastRoute\RouteCollector;

$request_uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];

$uri = parse_url($request_uri, PHP_URL_PATH);

// Create dispatcher and load routes
$dispatcher = FastRoute\simpleDispatcher(function(RouteCollector $r) {
    // Load API routes
    $apiRoutes = require __DIR__ . '/routes/api.php';
    $apiRoutes($r);
    
    // Load web routes
    $webRoutes = require __DIR__ . '/routes/web.php';
    $webRoutes($r);
});
The index.php file should remain clean and focused on dispatching. Move route definitions to separate files in the routes/ directory.

routes/ Directory

The routes/ directory contains your route definitions, organized by type:
  • api.php - API endpoints that return JSON responses
  • web.php - Web routes that return HTML views
This separation makes it easier to:
  • Apply different middleware or headers
  • Manage API versioning
  • Keep related routes together

Handler Classes

Handler classes (like v1/test.php) contain reusable logic for processing requests. This keeps your route definitions clean and promotes code reuse.

views/ Directory

The views/ directory stores your HTML templates and view files that are rendered for web routes.

Benefits of This Structure

Separation of Concerns

Routes, handlers, and views are separated into logical directories

Scalability

Easy to add new route files as your application grows

Maintainability

Clear organization makes code easier to find and update

Team Collaboration

Multiple developers can work on different route files simultaneously

Next Steps

API Routes

Learn how to define API routes

Web Routes

Learn how to define web routes

Route Handlers

Understand different handler types

Organizing Routes

Best practices for organizing routes

Build docs developers (and LLMs) love