I have been experimenting with the current version of the wonderful package for routing from thephpleague.
I have a simple index page to handle the routing and it works just fine.
<?php declare(strict_types=1);
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Laminas\Diactoros\ResponseFactory;
use League\Route\Router;
use League\Route\Strategy\JsonStrategy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
$strategy = new JsonStrategy(new ResponseFactory());
$router = new Router;
$router->setStrategy($strategy);
$router->post(
'/hello',
function (ServerRequestInterface $request): ResponseInterface
{
var_dump($request->getParsedBody());
}
);
$request = ServerRequestFactory::fromGlobals(
$_SERVER,
$_GET,
$_POST,
$_COOKIE,
$_FILES
);
$response = $router->dispatch($request);
// send the response to the browser
(new SapiEmitter)->emit($response);
My problem is when I send a POST
request with JSON
data in it, I am unable to capture it in my code as indicated by the line var_dump($request->getParsedBody());
.
At the moment, the only way I am able to capture the data sent with the request is by running json_decode(file_get_contents('php://input'), true);
.
What I do need to do in order to get the payload sent with whatever request, be it GET
, POST
, PUT
, PATCH
, or otherwise?
Thank you.