diff options
Diffstat (limited to '')
-rw-r--r-- | src/web/core/controller.php | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/web/core/controller.php b/src/web/core/controller.php index ac1e458..ca892e2 100644 --- a/src/web/core/controller.php +++ b/src/web/core/controller.php @@ -39,4 +39,27 @@ abstract class Controller extends Component { die(); } + /** + * Returns HTTP POST information if POST request. + * Returns 405 Method Not Allowed if not. + * + * If $key is specified, returns only that key. otherwise + * returns HTTP 400 Bad Request; + */ + protected function post_data(?string $key = NULL): array|string + { + // only post requests allowed + if ($_SERVER['REQUEST_METHOD'] != 'POST') + $this->error(405); + + // return entire $_POST array + if (!$key) + return $_POST; + + if (!isset($_POST[$key])) + $this->error(400); + + return $_POST[$key]; + } + } |