Skip to content

Commit cd9fcd0

Browse files
author
Phil Sturgeon
committed
Make Input::json() more logical
Now you can send JSON payloads as an actual body in a PUT or POST request, instead of only decoding a specific POST param, or whatever it was doing before. Also, if a non-JSON mime-type is sent it will refuse to try and parse the data, instead only returning the default value. This to me seems like a much more logical usage of Input::json() than the initial implementation.
1 parent 075781c commit cd9fcd0

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/Illuminate/Http/Request.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,15 +379,21 @@ public function replace(array $input)
379379
/**
380380
* Get the JSON payload for the request.
381381
*
382-
* @return object
382+
* @param string $key
383+
* @param mixed $default
384+
* @return string
383385
*/
384-
public function json()
386+
public function json($key = null, $default = null)
385387
{
386-
$arguments = func_get_args();
388+
$mime = $this->retrieveItem('server', 'CONTENT_TYPE', null);
387389

388-
array_unshift($arguments, $this->getContent());
390+
if (strpos($mime, '/json') === false) {
391+
return $default;
392+
}
393+
394+
$json = json_decode($this->getContent(), true);
389395

390-
return call_user_func_array('json_decode', $arguments);
396+
return array_get($json, $key, $default);
391397
}
392398

393399
/**

0 commit comments

Comments
 (0)