Created
September 16, 2016 18:28
-
-
Save albertomario/e15283ec814d097c860152d8555761e1 to your computer and use it in GitHub Desktop.
router example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Router { | |
private static $routes = array(); | |
private function __construct() {} | |
private function __clone() {} | |
public static function addRoute($routePattern, $callback) | |
{ | |
$pattern = '/^' . str_replace('/', '\/', $routePattern) . '$/'; | |
self::$routes[$pattern] = $callback; | |
} | |
public static function getRoutes() | |
{ | |
return self::$routes; | |
} | |
public static function dispatchApplication() | |
{ | |
foreach (self::$routes as $pattern => $callback) { | |
if(preg_match($pattern, $_SERVER['REQUEST_URI'], $params)) { | |
array_shift($params); | |
$callback = explode('@', $callback); | |
$instance = 'Endpoints\\'.$callback['0']; | |
return call_user_func_array([new $instance, $callback['1']], $params); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment