mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-10 16:42:11 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| de14a957d5 | |||
| c1c25e19ff | |||
| d61cbb73e5 | |||
| dc7faf52c0 | |||
| 29ab0e85ae | |||
| 539e905b45 | |||
| 0ab15839a9 | |||
| 5977efc942 |
@@ -267,29 +267,21 @@ class Input
|
|||||||
*/
|
*/
|
||||||
public function all(array $filter = null)
|
public function all(array $filter = null)
|
||||||
{
|
{
|
||||||
$output = $_POST;
|
$output = $_GET;
|
||||||
|
|
||||||
if ($this->request->getMethod() === 'post') {
|
if ($this->request->getMethod() === 'post') {
|
||||||
|
|
||||||
$contents = file_get_contents('php://input');
|
$contents = file_get_contents('php://input');
|
||||||
|
|
||||||
if (strpos(trim($contents), '{') === 0) {
|
if (strpos(trim($contents), '{') === 0) {
|
||||||
$output = json_decode($contents, true);
|
$post = json_decode($contents, true);
|
||||||
if ($output === false) {
|
if ($post !== false) {
|
||||||
$output = [];
|
$output = array_merge($output, $post);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = array_merge($_GET, $output);
|
return ($filter !== null) ? array_intersect_key($output, array_flip($filter)) : $output;
|
||||||
|
|
||||||
if ($filter !== null) {
|
|
||||||
$output = array_filter($output, function ($key) use ($filter) {
|
|
||||||
return (in_array($key, $filter) === true);
|
|
||||||
}, ARRAY_FILTER_USE_KEY);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@ class Request
|
|||||||
$this->host = $this->getHeader('http-host');
|
$this->host = $this->getHeader('http-host');
|
||||||
$this->uri = $this->getHeader('request-uri');
|
$this->uri = $this->getHeader('request-uri');
|
||||||
$this->input = new Input($this);
|
$this->input = new Input($this);
|
||||||
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method'), 'post'));
|
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method')));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseHeaders()
|
protected function parseHeaders()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Pecee\Http;
|
namespace Pecee\Http;
|
||||||
|
|
||||||
class Response
|
class Response
|
||||||
@@ -82,14 +83,20 @@ class Response
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Json encode array
|
* Json encode
|
||||||
* @param array $value
|
* @param array|\JsonSerializable $value
|
||||||
|
* @param int $options JSON options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
|
||||||
|
* @param int $dept JSON debt.
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function json(array $value)
|
public function json($value, $options = null, $dept = 512)
|
||||||
{
|
{
|
||||||
$this->header('Content-Type: application/json');
|
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
||||||
echo json_encode($value);
|
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
||||||
die();
|
}
|
||||||
|
$this->header('Content-Type: application/json; charset=utf-8');
|
||||||
|
echo json_encode($value, $options, $dept);
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ abstract class Route implements IRoute
|
|||||||
|
|
||||||
/* Only take matched parameters with name */
|
/* Only take matched parameters with name */
|
||||||
foreach ($parameters[1] as $name) {
|
foreach ($parameters[1] as $name) {
|
||||||
$values[$name] = isset($matches[$name]) ? $matches[$name] : null;
|
$values[$name] = (isset($matches[$name]) && $matches[$name] !== '') ? $matches[$name] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $values;
|
return $values;
|
||||||
|
|||||||
Reference in New Issue
Block a user