1

I've implemented Alto Router in my MVC framework and it's routing Get requests using the URI perfectly.

It's got to the point where i need to start dealing with forms and POST data, so ive set up a route to deal with a POST request, the request works and i get taken to the post requests route e.g domain.com/contact/send/

Unfortunately i don't get the POST data in the params array on the match.

I'm not sure if im getting myself confused as to whether the router should do this or not.

If the router is not supposed to handle getting the POST data, could you point me in the right direction of how i could ideally handle the POST data?

If the router is supposed to handle POST data, home comes i can't see it in the params array of the match?

Here's a snippet of the POST request:

$router->map('POST','/contact/send/,'contact#send', 'contact_form_send');
$match = $router->match()

Any help will be much appreciated, thanks

Tom Burman
  • 947
  • 3
  • 15
  • 37

3 Answers3

1

Here is how I do it.

$router->map('POST', '/companies/create', function() {
    if isset($_POST['company'])) {
        createCompany($_POST['company']); 
    }
});

hope it helps

istavros
  • 138
  • 5
1

If you take a look at the AltoRouter source, params is extracted from the request URL, so it will not contain any POST data. This parameter is mainly used by AltoRouter for its reversed routing feature, but you can use it instead of accessing $_GET directly.

If you need to access POST data you will have to get it from the request directly by using either the $_POST or $_REQUEST superglobal.

Note that AltoRouter will overwrite $_REQUEST when you call match so it may behave differently than expected since it will not contain cookies.

nullability
  • 10,545
  • 3
  • 45
  • 63
1

I get POST data through php://input

// update a document
$router->map('POST', '/docs', function () {
    $rc = new DocumentController();
    $data = json_decode(file_get_contents('php://input'));
    $data = (array)$data;
    $rc->updateDocument($data);
});