Welcome Guest, Not a member yet? Register   Sign In
Access form-data parameters from a ResourceController method
#1

(This post was last modified: 08-09-2022, 11:40 AM by kabeza.)

Hi
I've the following route group configured

Code:
$routes->group('api/project', ['namespace' => 'App\Controllers\Api', 'filter' => 'apikey'], function ($routes) {
    $routes->get('/', 'Project::index');
    $routes->get('/title', 'Project::title');
    $routes->get('(:num)', 'Project::show/$1');
    $routes->post('create', 'Project::create');
    $routes->put('update/(:num)', 'Project::update/$1');
});

However, the main route from the controller, the "/" (GET method) which calls Project::index can/must receive a parameter from body -> form-data

Here's my index method from that controller, but cannot access any parameter from form-data using getPost, getGet, etc.

Code:
    public function index()
    {
        $request = \Config\Services::request();
        $post = $request->getPost();
        $get = $request->getGet();
        $ri = $request->getRawInput();
        $gv = $request->getVar();

        return $this->respond($post, $get, $ri, $gv); // error on purpose, I've "echoed" all variables, without luck
    }

The only way to get the data is with getRawInput, but I get it in a very strange format

Code:
{
  "----------------------------310795567447521306068169\r\nContent-Disposition:_form-data;_name": "\"idproject\"\r\n\r\n32\r\n----------------------------310795567447521306068169--\r\n"
}

Is there a clean way to get the parameters and values from body->form-data in a get method from a ResourceController ?

Thanks
Reply
#2

See https://www.php.net/manual/en/features.f...method.php
Reply
#3

The data is encoded with multipart/form-data. This data encoding is used for uploading files.
Don't use it for the GET method and you'll be fine.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB