diff --git a/src/Router.php b/src/Router.php index ed3d660..129bf36 100755 --- a/src/Router.php +++ b/src/Router.php @@ -44,6 +44,13 @@ class Router */ private $basePath = ''; + /** + * Callback called before dispatch. Should return the route to dispatch. + * The choosen route for dispatch is the only parameter. + * @var callable + */ + private static $shouldDispatchCallback = null; + /** * @param RouteCollection $collection */ @@ -140,6 +147,17 @@ public function match($requestUrl, $requestMethod = RequestMethodInterface::METH } $routes->setParameters($params); + + $shouldDispatchCallback = static::$shouldDispatchCallback; + //If shouldDispatch callback set, call it before dispatch + if($shouldDispatchCallback !== null){ + $routes = $shouldDispatchCallback($routes); + //if false, ignore the route + if(!$routes){ + continue; + } + } + $routes->dispatch(); return $routes; @@ -209,4 +227,14 @@ public static function parseConfig(array $config) return $router; } + + /** + * Sets a callback before dispatch to allow routing or not. + * The callable takes the dispatching route as first parameter. + * It should return false to skip the route, or a Route in replacement of the passed Route. + * @param callable $shouldDispatchCallback + */ + public static function setShouldDispatchCallback(callable $shouldDispatchCallback){ + static::$shouldDispatchCallback = $shouldDispatchCallback; + } }