Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}