Implementation of Router
using a PSR-16 cache.
class CachedRouter extends SimpleRouter
{
// Methods
public function __construct(CacheInterface $cache, string $cacheKey = 'routerData', $driver = Router::DRIVER_GROUP_COUNT, array $routes = [], bool $cacheDisable = false)
public function add($methods, string $pattern, $handler): Router
public function addGroup(string $patternPrefix, array $routes): Router
public function connect(string $pattern, $handler): Router
public function delete(string $pattern, $handler): Router
public function dispatch(RequestInterface $request, bool $exceptions = false): array
public function get(string $pattern, $handler): Router
public function head(string $pattern, $handler): Router
public function options(string $pattern, $handler): Router
public function patch(string $pattern, $handler): Router
public function post(string $pattern, $handler): Router
public function purge(string $pattern, $handler): Router
public function put(string $pattern, $handler): Router
public function refreshCache(): bool
public function trace(string $pattern, $handler): Router
}
Extends SimpleRouter
.
- __construct
- add
- addGroup
- connect
- delete
- dispatch
- get
- head
- options
- patch
- post
- purge
- put
- refreshCache
- trace
Builds a CachedRouter
instance.
__construct(CacheInterface $cache, string $cacheKey = 'routerData', $driver = Router::DRIVER_GROUP_COUNT, array $routes = [], bool $cacheDisable = false)
-
CacheInterface
$cacheRequired. PSR-16 cache to use.
-
string
$cacheKeyOptional. Defaults to
'routerData'
. Key to use for cached data. -
string
$driverOptional. Defaults to
Router::DRIVER_GROUP_COUNT
. FastRoute driver. You should use theRouter::DRIVER_*
constants. -
array
$routesOptional. Defaults to
[]
. Array ofadd()
parameter values. -
bool
$cacheDisableOptional. Defaults to
false
. Disable cache functionality.
-
Driver is invalid.
-
Routes array is invalid.
CachedRouter
Generates dispatch data and caches it.
public function refreshCache(): bool
This is useful if the routes configuration has changed, for example.
Note this method will still work when $cacheDisable = true
has been passed
to the constructor.
None.
Nothing is thrown.
-
bool
The result of the
CacheInterface::set()
call.