mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 13:22:14 +00:00
@@ -27,7 +27,7 @@ class Request {
|
|||||||
$this->host = $_SERVER['HTTP_HOST'];
|
$this->host = $_SERVER['HTTP_HOST'];
|
||||||
$this->uri = $_SERVER['REQUEST_URI'];
|
$this->uri = $_SERVER['REQUEST_URI'];
|
||||||
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : strtolower($_SERVER['REQUEST_METHOD']);
|
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : strtolower($_SERVER['REQUEST_METHOD']);
|
||||||
$this->headers = getallheaders();
|
$this->headers = array_change_key_case(getallheaders(), CASE_LOWER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,7 +105,7 @@ class Request {
|
|||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getHeader($name) {
|
public function getHeader($name) {
|
||||||
return (isset($this->headers[$name])) ? $this->headers[$name] : null;
|
return (isset($this->headers[strtolower($name)])) ? $this->headers[strtolower($name)] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ class RouterBase {
|
|||||||
|
|
||||||
$newPrefixes = $prefixes;
|
$newPrefixes = $prefixes;
|
||||||
$mergedSettings = array_merge($settings, $route->getMergeableSettings());
|
$mergedSettings = array_merge($settings, $route->getMergeableSettings());
|
||||||
|
|
||||||
if($route->getPrefix()) {
|
if($route->getPrefix()) {
|
||||||
array_push($newPrefixes, rtrim($route->getPrefix(), '/'));
|
array_push($newPrefixes, rtrim($route->getPrefix(), '/'));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,26 +10,26 @@ abstract class RouterEntry {
|
|||||||
const REQUEST_TYPE_POST = 'post';
|
const REQUEST_TYPE_POST = 'post';
|
||||||
const REQUEST_TYPE_GET = 'get';
|
const REQUEST_TYPE_GET = 'get';
|
||||||
const REQUEST_TYPE_PUT = 'put';
|
const REQUEST_TYPE_PUT = 'put';
|
||||||
|
const REQUEST_TYPE_PATCH = 'patch';
|
||||||
const REQUEST_TYPE_DELETE = 'delete';
|
const REQUEST_TYPE_DELETE = 'delete';
|
||||||
|
|
||||||
public static $allowedRequestTypes = array(
|
public static $allowedRequestTypes = array(
|
||||||
self::REQUEST_TYPE_DELETE,
|
self::REQUEST_TYPE_DELETE,
|
||||||
self::REQUEST_TYPE_GET,
|
self::REQUEST_TYPE_GET,
|
||||||
self::REQUEST_TYPE_POST,
|
self::REQUEST_TYPE_POST,
|
||||||
self::REQUEST_TYPE_PUT
|
self::REQUEST_TYPE_PUT,
|
||||||
|
self::REQUEST_TYPE_PATCH
|
||||||
);
|
);
|
||||||
|
|
||||||
protected $settings;
|
protected $settings;
|
||||||
protected $callback;
|
protected $callback;
|
||||||
protected $parameters;
|
protected $parameters;
|
||||||
protected $parametersRegex;
|
|
||||||
protected $regexMatch;
|
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->settings = array();
|
$this->settings = array();
|
||||||
$this->settings['requestMethods'] = array();
|
$this->settings['requestMethods'] = array();
|
||||||
|
$this->settings['parametersRegex'] = array();
|
||||||
$this->parameters = array();
|
$this->parameters = array();
|
||||||
$this->parametersRegex = array();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -245,13 +245,25 @@ abstract class RouterEntry {
|
|||||||
|
|
||||||
public function loadMiddleware(Request $request) {
|
public function loadMiddleware(Request $request) {
|
||||||
if($this->getMiddleware()) {
|
if($this->getMiddleware()) {
|
||||||
$middleware = $this->loadClass($this->getMiddleware());
|
if(is_array($this->getMiddleware())) {
|
||||||
if (!($middleware instanceof IMiddleware)) {
|
foreach($this->getMiddleware() as $middleware) {
|
||||||
throw new RouterException($this->getMiddleware() . ' must be instance of Middleware');
|
$middleware = $this->loadClass($middleware);
|
||||||
}
|
if (!($middleware instanceof IMiddleware)) {
|
||||||
|
throw new RouterException($middleware . ' must be instance of Middleware');
|
||||||
|
}
|
||||||
|
|
||||||
/* @var $class Middleware */
|
/* @var $class Middleware */
|
||||||
$middleware->handle($request);
|
$middleware->handle($request);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$middleware = $this->loadClass($this->getMiddleware());
|
||||||
|
if (!($middleware instanceof IMiddleware)) {
|
||||||
|
throw new RouterException($this->getMiddleware() . ' must be instance of Middleware');
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @var $class Middleware */
|
||||||
|
$middleware->handle($request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class SimpleRouter {
|
|||||||
public static function put($url, $callback, array $settings = null) {
|
public static function put($url, $callback, array $settings = null) {
|
||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
$route->addSettings($settings);
|
$route->addSettings($settings);
|
||||||
$route->setRequestMethods(array(RouterRoute::REQUEST_TYPE_PUT));
|
$route->setRequestMethods(array(RouterRoute::REQUEST_TYPE_PUT, RouterRoute::REQUEST_TYPE_PATCH));
|
||||||
|
|
||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
|
|||||||
Reference in New Issue
Block a user