1
0
mirror of https://github.com/PurpleI2P/regi2p.git synced 2025-01-15 13:59:56 +00:00
regi2p/lib/router.php
r4sas 67655b7501
move routing defining inside class
Signed-off-by: r4sas <r4sas@i2pmail.org>
2022-06-12 00:00:24 +00:00

76 lines
2.2 KiB
PHP

<?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';
});
}
}