1

Currently, I am doing this.

$responses = array(
        "E" => 0,
        "I" => 0);

foreach ($_POST as $key => $value) {
    if (strlen($value) == 1) {
        ++$responses[$value];
    }
}

Is there a way to use craft()->requests for this? I tried

foreach (craft()->request->getPost() as $key => $value) {
    if (strlen($value) == 1) {
        ++$responses[$value];
    }
}

But this did not seem to work.

3

I wouldn't consider it as good practice to do what you want, there are good reasons why something like this is not possible by default and why all functions to get all parameters are private.

A better way is to send your parameters inside an array and fetch this or you can create an array of all possible parameters and loop it

$secureParameters = ['A', 'B', 'C'];
$request = Craft::$app->getRequest();
$values = [];
foreach($secureParameters as $key){
    $values[$key] = $request->getBodyParam($key);
}

If you really don't care about receiving unexpected parameters and raising exceptions you can also loop your $_POST directly foreach($_POST as $key => $v){ and fetch the $request->getBodyParam($key) at least

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.