From d1b23d3f2e743d6b0e98972526ad030f2b3b00dd Mon Sep 17 00:00:00 2001 From: Ryan Mahoney Date: Thu, 10 Nov 2016 09:46:11 -0500 Subject: [PATCH] read json from request body provide it as first argument to controller --- src/Service.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Service.php b/src/Service.php index 59d4fc2..f3f5cf7 100644 --- a/src/Service.php +++ b/src/Service.php @@ -339,6 +339,19 @@ public function execute(Array $callable, Array $parameters = [], Array $beforeAc return; } + private function getJsonInput () : array + { + $input = file_get_contents('php://input'); + if ($input === false || $input == '') { + return []; + } + $input = json_decode($input, true); + if (json_last_error() == JSON_ERROR_NONE) { + return []; + } + return $json; + } + public function run($method = false, $path = false, &$code = false) { $originalGet = $_GET; @@ -364,7 +377,8 @@ public function run($method = false, $path = false, &$code = false) try { http_response_code(200); ob_start(); - $response = $this->execute($route[1], $route[2]); + $input = array_unshift($route[2], $this->getJsonInput()); + $response = $this->execute($route[1], $input); if ($response === false) { $output = false; } else {