Reduced visual clutter for routing with some helpers

This commit is contained in:
Sky Johnson 2024-12-13 16:35:05 -06:00
parent f80cb7cd5b
commit 67045c8c8d
7 changed files with 54 additions and 38 deletions

View File

@ -40,8 +40,7 @@ $r->get('/spell/:id', 'healspells');
$r->get('/showchar', 'showchar');
$r->get('/onlinechar/:id', 'onlinechar');
$r->get('/showmap', 'showmap');
$r->get('/babblebox', 'babblebox');
$r->post('/babblebox', 'babblebox');
$r->form('/babblebox', 'babblebox');
// [code, handler, params, middleware]
$l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

View File

@ -11,31 +11,31 @@ function register_routes(Router $r): Router
global $userrow;
if (isset($userrow) && $userrow !== false && $userrow['authlevel'] === 1) {
$r->get('/admin', 'Admin\donothing');
$r->get('/admin/main', 'Admin\primary');
$r->post('/admin/main', 'Admin\primary');
$r->form('/admin/main', 'Admin\primary');
$r->get('/admin/items', 'Admin\items');
$r->get('/admin/items/:id', 'Admin\edititem');
$r->post('/admin/items/:id', 'Admin\edititem');
$r->form('/admin/items/:id', 'Admin\edititem');
$r->get('/admin/drops', 'Admin\drops');
$r->get('/admin/drops/:id', 'Admin\editdrop');
$r->post('/admin/drops/:id', 'Admin\editdrop');
$r->form('/admin/drops/:id', 'Admin\editdrop');
$r->get('/admin/towns', 'Admin\towns');
$r->get('/admin/towns/:id', 'Admin\edittown');
$r->post('/admin/towns/:id', 'Admin\edittown');
$r->form('/admin/towns/:id', 'Admin\edittown');
$r->get('/admin/monsters', 'Admin\monsters');
$r->get('/admin/monsters/:id', 'Admin\editmonster');
$r->post('/admin/monsters/:id', 'Admin\editmonster');
$r->form('/admin/monsters/:id', 'Admin\editmonster');
$r->get('/admin/level', 'Admin\levels');
$r->get('/admin/level/:id', 'Admin\editlevel');
$r->post('/admin/level/:id', 'Admin\editlevel');
$r->form('/admin/level/:id', 'Admin\editlevel');
$r->get('/admin/spells', 'Admin\spells');
$r->get('/admin/spells/:id', 'Admin\editspell');
$r->post('/admin/spells/:id', 'Admin\editspell');
$r->form('/admin/spells/:id', 'Admin\editspell');
$r->get('/admin/users', 'Admin\users');
$r->get('/admin/users/:id', 'Admin\edituser');
$r->post('/admin/users/:id', 'Admin\edituser');
$r->get('/admin/news', 'Admin\addnews');
$r->post('/admin/news', 'Admin\addnews');
$r->form('/admin/users/:id', 'Admin\edituser');
$r->form('/admin/news', 'Admin\addnews');
}
return $r;
}

View File

@ -8,11 +8,9 @@ use Router;
function register_routes(Router $r): Router
{
$r->get('/fight', 'Fights\fight');
$r->post('/fight', 'Fights\fight');
$r->form('/fight', 'Fights\fight');
$r->get('/victory', 'Fights\victory');
$r->get('/drop', 'Fights\drop');
$r->post('/drop', 'Fights\drop');
$r->form('/drop', 'Fights\drop');
$r->get('/dead', 'Fights\dead');
return $r;
}

View File

@ -9,8 +9,7 @@ use Router;
function register_routes(Router $r): Router
{
$r->get('/forum/thread/:x/:x', 'Forum\showthread');
$r->get('/forum/new', 'Forum\newthread');
$r->post('/forum/new', 'Forum\newthread');
$r->form('/forum/new', 'Forum\newthread');
$r->post('/forum/reply', 'Forum\reply');
$r->get('/forum/list/:x', 'Forum\donothing');
$r->get('/forum', 'Forum\donothing');

View File

@ -8,8 +8,7 @@ use Router;
function register_routes(Router $r): Router
{
$r->get('/inn', 'Towns\inn');
$r->post('/inn', 'Towns\inn');
$r->form('/inn', 'Towns\inn');
$r->get('/buy', 'Towns\buy');
$r->get('/buy2/:id', 'Towns\buy2');
$r->post('/buy3/:id', 'Towns\buy3');

View File

@ -6,17 +6,12 @@ use Router;
function register_routes(Router $r): Router
{
$r->get('/login', 'Users\login');
$r->post('/login', 'Users\login');
$r->form('/login', 'Users\login');
$r->get('/logout', 'Users\logout');
$r->get('/register', 'Users\register');
$r->post('/register', 'Users\register');
$r->get('/lostpassword', 'Users\lostpassword');
$r->post('/lostpassword', 'Users\lostpassword');
$r->get('/changepassword', 'Users\changepassword');
$r->post('/changepassword', 'Users\changepassword');
$r->get('/verify', 'Users\verify');
$r->post('/verify', 'Users\verify');
$r->form('/register', 'Users\register');
$r->form('/lostpassword', 'Users\lostpassword');
$r->form('/changepassword', 'Users\changepassword');
$r->form('/verify', 'Users\verify');
return $r;
}

View File

@ -151,6 +151,32 @@ class Router
return $this->add('PATCH', $route, $handler);
}
/**
* Register multiple verbs to the same route.
*/
public function many(array $methods, string $route, callable $handler): Router
{
foreach ($methods as $method) $this->add($method, $route, $handler);
return $this;
}
/**
* Register all verbs to the same route.
*/
public function any(string $route, callable $handler): Router
{
foreach (SELF::VALID_METHODS as $method) $this->add($method, $route, $handler);
return $this;
}
/**
* Some pages function entirely as forms; thus we can shorthand a GET/POST route.
*/
public function form(string $route, callable $handler): Router
{
return $this->many(['GET', 'POST'], $route, $handler);
}
/**
* Validate the given method against valid HTTP verbs.
*/