-
Notifications
You must be signed in to change notification settings - Fork 0
Caching
Muhammet Şafak edited this page Jun 9, 2026
·
1 revision
Building the route table on every request is cheap, but for large applications you can cache the compiled table to a file and skip re-registration.
$router = new Router($request, $response, [
'cache' => [
'enable' => true,
'path' => __DIR__ . '/var/cache/routes.cache',
'ttl' => 86400, // seconds (default: 1 day)
],
]);- On construction, if a fresh (non-expired) cache file exists, the route table is loaded from it and the router is marked as cached.
- While the router is cached, registration calls (
get(),group(), …) are no-ops — the routes already came from the cache. - When the router is destroyed and it was not loaded from cache, the current route table is written to the cache file.
A typical front controller registers routes unconditionally; on the first request they are compiled and cached, and on subsequent requests (until the TTL expires) they are loaded from the cache:
$router = new Router($request, $response, [
'cache' => ['enable' => true, 'path' => $cacheFile],
]);
// These run normally the first time, and are skipped once cached.
$router->get('/', 'HomeController@index');
$router->resource('photos', 'PhotoController');
$response = $router->dispatch();-
Handlers must be cacheable. String (
'Controller@method') and array handlers cache fine. Closures cannot be serialized, so don't enable caching if your routes use closure handlers. - Delete the cache file (or wait for the TTL) after changing your routes.
- A write failure is reported as a warning and never interrupts the response.
- The cache is read with
allowed_classes: false, so it never instantiates arbitrary objects from the cache file.
Next: API Reference.
initphp/router · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Defining Routes
Handling Requests
Reference
Migration