Skip to content

Instantly share code, notes, and snippets.

@albertomario
Created September 16, 2016 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertomario/243939dba3cb1e3f3a5a69defe29d7f1 to your computer and use it in GitHub Desktop.
Save albertomario/243939dba3cb1e3f3a5a69defe29d7f1 to your computer and use it in GitHub Desktop.
router example
<?php
namespace Phasic\Core;
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