PHP Flight Micro framework POST data as array

5k Views Asked by At

I'm using the PHP Flight micro framework (http://flightphp.com/). On a POST request, the variables can be retrieved by using Flight::request()->data (http://flightphp.com/learn#requests). As is, it appears to be typed: flight\util\Collection Object. As I am passing this data to another class, I would like to convert it to a standard associative array. I can simply foreach through the data, but is there a better way? What is the best method to achieve this? Am I asking the wrong question?

4

There are 4 best solutions below

0
On

You can get the internal array by using Collection::getData():

Flight::request()->data->getData();
0
On

You can convert the flight\util\Collection Object to an array by casting it to an array.

For example try:

$myArray = (array)Flight::request()->data;
// Sometimes you need to pop the first element
$myArray = array_pop($myArray);
0
On

You can just use $_POST[], as of standard php. https://www.php.net/manual/en/reserved.variables.post.php

0
On

Yau don't need another class, you can use self-PHP global varriable, it is easy

I'm using that like;

<?php
Flight::route('POST /post-meta', function(){
    print_r($_POST);
});
?>

JSON sample;

<?php
Flight::route('POST /report', function(){
    if(isset($_POST['reportcode'])){
        $id = (int)base64_decode($_POST['reportcode']);
        if(Flight::db()->count() == 0){
            $return['status'] = "ok";
            $return['content'] = "<b>Succesfuly</b> sent your report this link"; //lang
        }else{
            $return['status'] = "already";
            $return['content'] = "<b>Already</b> this link reported"; //lang
        }
    }else{
        $return['status'] = 0;
    }
    echo json_encode($return);
});
?>