I'm currently creating a utility that is heavily AJAX orientated. I have a page called ajax.php which is handling all POST requests. It switches by the provided command, checks all the arguments exist then passes it off to the relevant class e.g.
case 'addperson':
$name = $_POST['name'] ?? die('{"result":0, "data":"name not provided"}');
$email = $_POST['email'] ?? die('{"result":0, "data":"email not provided"}');
try {
(new Person(new DB()))->addPerson($name, $email);
}
catch (Exception $e) {
exit("{\"result\":0, \"data\":\"{$e->getMessage()}\"}");
}
exit('{"result":1}');
This works fine but was wondering how people generally go about handling big lists of $_POST arguments. Would you do the above, potentially have 20 lines of $arg = $_POST['arg'] or perhaps something like,
foreach (['name', 'state'] AS $arg) {
$$arg = $_POST[$arg] ?? die("{\"result\":0, \"data\":\"$arg not provided\"}");
}
The other option is to not check it exists at all.
list($a, $b, $c) = [$_POST['a'], $_POST['b'], $_POST['c']];
Since I'm the only one working on this project, i can ensure i always provide the necessary data but this just seems bad and may create future issues (especially if I get a few helpers!)
Finally, what do you think about having the ajax.php confirm all arguments have been provided (not validated) rather than just checking for the command and passing off the $_POST array to a class which will then confirm all arguments have been provided?