diff --git a/public/index.php b/public/index.php index 6263fa0..dad8448 100644 --- a/public/index.php +++ b/public/index.php @@ -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']); diff --git a/src/actions/admin.php b/src/actions/admin.php index 971df47..2dac18b 100644 --- a/src/actions/admin.php +++ b/src/actions/admin.php @@ -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; } diff --git a/src/actions/fight.php b/src/actions/fight.php index 0c58e1c..b9b8594 100644 --- a/src/actions/fight.php +++ b/src/actions/fight.php @@ -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; } diff --git a/src/actions/forum.php b/src/actions/forum.php index 2b95453..e358455 100644 --- a/src/actions/forum.php +++ b/src/actions/forum.php @@ -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'); diff --git a/src/actions/towns.php b/src/actions/towns.php index 6383507..0eb1dbf 100644 --- a/src/actions/towns.php +++ b/src/actions/towns.php @@ -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'); diff --git a/src/actions/users.php b/src/actions/users.php index d90c731..16b2503 100644 --- a/src/actions/users.php +++ b/src/actions/users.php @@ -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; } diff --git a/src/router.php b/src/router.php index 2547927..43073ff 100644 --- a/src/router.php +++ b/src/router.php @@ -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. */