@@ -271,3 +271,82 @@ $container['FastRoute\RouteCollector'] = new FastRouteCollectorFactory();
271271$container['FastRoute\RouteDispatcher'] = new FastRouteDispatcherFactory();
272272$container['Zend\Expressive\Router\RouterInterface'] = new RouterFactory();
273273```
274+
275+ ### FastRoute caching support
276+
277+ - Since zend-expressive-fastroute 1.3.0.
278+
279+ Starting from version 1.3.0, zend-expressive-fastroute comes with support
280+ for FastRoute native dispatch data caching.
281+
282+ Enabling this feature requires changes to your configuration. Typically, router
283+ configuration occurs in ` config/autoload/routes.global.php ` ; as such, we will
284+ reference that file when indicating configuration changes.
285+
286+ The changes required are:
287+
288+ - You will need to delegate creation of the router instance to a new factory.
289+
290+ - You will need to add a new configuration entry, ` $config['router']['fastroute'] ` .
291+ The options in this entry will be used by the factory to build the router
292+ instance in order to toggle caching support and to specify a custom cache
293+ file.
294+
295+ As an example:
296+
297+ ``` php
298+ // File config/autoload/routes.global.php
299+
300+ return [
301+ 'dependencies' => [
302+ //..
303+ 'invokables' => [
304+ /* ... */
305+ // Comment out or remove the following line:
306+ // Zend\Expressive\Router\RouterInterface::class => Zend\Expressive\Router\FastRouteRouter::class,
307+ /* ... */
308+ ],
309+ 'factories' => [
310+ /* ... */
311+ // Add this line; the specified factory now creates the router instance:
312+ Zend\Expressive\Router\RouterInterface::class => Zend\Expressive\Router\FastRouteRouterFactory::class,
313+ /* ... */
314+ ],
315+ ],
316+
317+ // Add the following to enable caching support:
318+ 'router' => [
319+ 'fastroute' => [
320+ // Enable caching support:
321+ 'cache_enabled' => true,
322+ // Optional (but recommended) cache file path:
323+ 'cache_file' => 'data/cache/fastroute.php.cache',
324+ ],
325+ ],
326+
327+ 'routes' => [ /* ... */ ],
328+ ]
329+ ```
330+
331+ The FastRoute-specific caching options are as follows:
332+
333+ - ` cache_enabled ` (bool) is used to toggle caching support. It's advisable to enable
334+ caching in a production environment and leave it disabled for the development
335+ environment. Commenting or omitting this option is equivalent to having it set
336+ to ` false ` . We recommend enabling it in ` config/autoload/routes.global.php ` ,
337+ and, in development, disabling it within ` config/autoload/routes.local.php ` or
338+ ` config/autoload/local.php ` .
339+
340+ - ` cache_file ` (string) is an optional parameter that represents the path of
341+ the dispatch data cache file. It can be provided as an absolute file path or
342+ as a path relative to the zend-expressive working directory.
343+
344+ It defaults to ` data/cache/fastroute.php.cache ` , where ` data/cache/ ` is the
345+ cache directory defined within the zend-expressive skeleton application. An
346+ explicit absolute file path is recommended since the php ` include ` construct
347+ will skip searching the ` include_path ` and the current directory.
348+
349+ If you choose a custom path, make sure that the directory exists and is
350+ writable by the owner of the PHP process. As with any other zend-expressive
351+ cached configuration, you will need to purge this file in order to enable any
352+ newly added route when FastRoute caching is enabled.
0 commit comments