Functional Routing
Functional routing gives you complete control over the routing process using simple PHP functions. This is the core routing approach - all other styles (file-based, Router class) are built on top of these functions.
When to Use Functional Routing
- Full Control - You want direct access to all routing primitives
- Custom Logic - Need conditional routing based on complex criteria
- Centralized Routes - All routes in a single
index.phporroutes/web.php - Familiar Style - Similar to frameworks like Slim or raw PHP routing
- Minimal Overhead - No class autoloading, just functions
Basic Usage
Single Entry Point (index.php)
<?php
require_once 'vendor/autoload.php';
router(function() {
route(method(GET), url_path('/'), function() {
echo html_response(HTTP_OK, '<h1>Home</h1>');
});
route(method(GET), url_path('/about'), function() {
echo html_response(HTTP_OK, '<h1>About Us</h1>');
});
route(method(GET), url_path('/contact'), function() {
echo html_response(HTTP_OK, '<h1>Contact</h1>');
});
});
Route Files Structure
For larger applications, organize routes in separate files:
/app
├── public/
│ └── index.php # Entry point
├── routes/
│ ├── web.php # Web routes
│ └── api.php # API routes
└── vendor/
public/index.php:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
router(function() {
require __DIR__ . '/../routes/web.php';
require __DIR__ . '/../routes/api.php';
});
routes/web.php:
<?php
route(method(GET), url_path('/'), function() {
echo html_response(HTTP_OK, '<h1>Welcome</h1>');
});
route(method(GET), url_path('/about'), function() {
echo html_response(HTTP_OK, '<h1>About</h1>');
});
routes/api.php:
<?php
routerGroup('/api', function() {
route(method(GET), url_path('/users'), function() {
echo json_response(HTTP_OK, array('users' => array()));
});
route(method(POST), url_path('/users'), function() {
$data = json_body();
echo json_response(HTTP_CREATED, array('created' => $data));
});
});
Topics
- Getting Started - Installation and server configuration
- HTTP Methods - Match GET, POST, PUT, DELETE, etc.
- URL Matching - Static paths, wildcards, 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
- Advanced Patterns - Conditionals, priority, subsites
- PSR-7 Style - Request/Response objects
- Static Files - Serving static assets