Skip to content

Instantly share code, notes, and snippets.

@albertomario
Created September 16, 2016 18:29
Show Gist options
  • Save albertomario/605519f0fbaad2511174bb69f6ac36dd to your computer and use it in GitHub Desktop.
Save albertomario/605519f0fbaad2511174bb69f6ac36dd 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