Domain registry project http://reg.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.2 KiB

<?php
namespace App;
class Router {
private $routes;
private $errRoute;
public function addRoute($pattern, $function) {
$this->routes['{'.$pattern.'}'] = $function;
}
public function addErrorRoute($function) {
$this->errRoute = $function;
}
public function run() {
foreach ($this->routes as $pattern => $function) {
if (preg_match($pattern, $_SERVER['REQUEST_URI'], $params)) {
array_shift($params);
array_unshift($params, $_SERVER['REQUEST_URI']);
return call_user_func_array($function, array_values($params));
}
}
return call_user_func($this->errRoute);
}
public function loadRoutes () {
$this->addRoute('^/?(?:\?|$)', function($url) {
require __DIR__ . '/../views/home.php';
});
$this->addRoute('^/add/?(?:\?|$)', function($url) {
require __DIR__ . '/../views/add.php';
});
$this->addRoute('^/alive/?([0-9]+)?/?(?:\?|$)', function($url, $page = 1) {
require __DIR__ . '/../views/alive.php';
});
$this->addRoute('^/all/?([0-9]+)?/?', function($url, $page = 1) {
require __DIR__ . '/../views/all.php';
});
$this->addRoute('^/api/?([^\?/]*)/?([^\?/]*)?/?(?:\?|$)', function($url, $command, $query = "") {
require __DIR__ . '/../views/api.php';
});
$this->addRoute('^/jump/?([^\?/]*)/?(?:\?|$)', function($url, $query = "") {
require __DIR__ . '/../views/jump.php';
});
$this->addRoute('^/autojump/?(.*)/?', function($url, $query = "") {
require __DIR__ . '/../views/autojump.php';
});
$this->addRoute('^/latest/?(?:\?|$)', function($url) {
require __DIR__ . '/../views/latest.php';
});
$this->addRoute('^/search/?([^\?/]*)/?(?:\?|$)', function($url, $query = "") {
require __DIR__ . '/../views/search.php';
});
$this->addRoute('^/hidden/?([0-9]+)?/?(?:\?|$)', function($url, $page = 1) {
require __DIR__ . '/../views/hidden.php';
});
$this->addErrorRoute(function() {
require __DIR__ . '/../views/404.php';
});
}
}