File-Based Routing
File-based routing lets you add modern routing capabilities to existing PHP file structures without restructuring your project. Each PHP file handles its own routes based on its location in the filesystem.
When to Use File-Based Routing
- Legacy Projects - Adding routing to existing PHP sites
- Gradual Migration - Incrementally modernizing a codebase
- Simple APIs - Quick API endpoints in existing projects
- Shared Hosting - Environments where you can't control the web server
How It Works
The file_router() function automatically calculates the URL path based on the file's location relative to DOCUMENT_ROOT. You define routes relative to that path.
/wwwroot/
├── index.php → matches /
├── api/
│ ├── users.php → matches /api/users
│ └── posts.php → matches /api/posts
└── admin/
└── index.php → matches /admin/
Basic Usage
File: /wwwroot/api/users.php
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
file_router(function() {
// GET /api/users
route(method(GET), url_path('/'), function() {
echo json_response(HTTP_OK, array(
'users' => array(
array('id' => 1, 'name' => 'Alice'),
array('id' => 2, 'name' => 'Bob'),
)
));
});
// POST /api/users
route(method(POST), url_path('/'), function() {
$data = json_body();
echo json_response(HTTP_CREATED, array('user' => $data));
});
});
Directory Index Files
Files named index.php or default.php match their parent directory:
File: /wwwroot/admin/index.php
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
file_router(function() {
// GET /admin/ (matches the directory, not /admin/index)
route(method(GET), url_path('/'), function() {
echo html_response(HTTP_OK, '<h1>Admin Dashboard</h1>');
});
});
Topics
- Getting Started - Installation and server configuration
- HTTP Methods - Match GET, POST, PUT, DELETE, etc.
- URL Matching - Static paths and parameters
- Route Groups - Organize routes with prefixes
- Request Handling - JSON, forms, files, headers
- Response Helpers - JSON, HTML, downloads
- Error Handling - Exceptions and error responses
- Middleware - Authentication, logging, etc.
- Controllers - Closures, classes, and invokables
- Server Configuration - Detailed server setup
- PSR-7 Style - Request/Response objects
- Static Files - Serving static assets