mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Compare commits
46 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d338e9aa9 | |||
| 889ceaa37f | |||
| 363338c92f | |||
| 3dd9dba029 | |||
| be277f276f | |||
| f215eaa9cf | |||
| 933f2370fe | |||
| b3f8910cab | |||
| 8557741083 | |||
| c60d7d81c1 | |||
| 637b998f02 | |||
| aca7d3d503 | |||
| 846c9e6584 | |||
| 0df469184c | |||
| 649ed28a91 | |||
| 5cb7086e96 | |||
| a2edc1504c | |||
| d31cda8e70 | |||
| 921f050a31 | |||
| dcbf59b305 | |||
| bc16388613 | |||
| d3ed3a61b5 | |||
| 969b64650e | |||
| c2cf2334e7 | |||
| af730e6e15 | |||
| f5a32cf520 | |||
| 1843ea0594 | |||
| fd28f4549f | |||
| 5e5a424ee8 | |||
| 099f04fc10 | |||
| ac2993f804 | |||
| 99da70874e | |||
| 2a66350883 | |||
| c95a5291d3 | |||
| 20fc067765 | |||
| cbb4294f58 | |||
| d6bdcbe70c | |||
| 25f569384f | |||
| b37c73d5dd | |||
| f5597c24ce | |||
| b8061f2aa7 | |||
| 6c7ac2b250 | |||
| d2de22e5e0 | |||
| 252fb16326 | |||
| 63dfbb24af | |||
| 3ccfac9422 |
@@ -1,5 +1,5 @@
|
||||
# Simple PHP router
|
||||
Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.
|
||||
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the Laravel router.
|
||||
|
||||
## Installation
|
||||
Add the latest version pf Simple PHP Router to your ```composer.json```
|
||||
@@ -33,7 +33,7 @@ Add the latest version pf Simple PHP Router to your ```composer.json```
|
||||
|
||||
- Global Constraints
|
||||
- Sub-Domain Routing
|
||||
- Optional/required parameters
|
||||
- Optional parameters
|
||||
|
||||
## Initialising the router
|
||||
|
||||
@@ -269,4 +269,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
SOFTWARE.
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Pecee\\": "src/"
|
||||
"Pecee\\": "src/Pecee/"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
namespace Pecee\Exception;
|
||||
|
||||
class TokenMismatchException extends \Exception {}
|
||||
@@ -1,17 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\Http\Middleware;
|
||||
|
||||
use Pecee\CsrfToken;
|
||||
use Pecee\Exception\TokenMismatchException;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterException;
|
||||
|
||||
class BaseCsrfVerifier extends Middleware {
|
||||
class BaseCsrfVerifier implements IMiddleware {
|
||||
|
||||
const POST_KEY = 'csrf-token';
|
||||
const HEADER_KEY = 'X-CSRF-TOKEN';
|
||||
|
||||
protected $except;
|
||||
protected $csrfToken;
|
||||
|
||||
|
||||
public function __construct() {
|
||||
$this->csrfToken = new CsrfToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the url matches the urls in the except property
|
||||
@@ -52,9 +57,8 @@ class BaseCsrfVerifier extends Middleware {
|
||||
$token = $request->getHeader(self::HEADER_KEY);
|
||||
}
|
||||
|
||||
$tokenValidator = new CsrfToken();
|
||||
if( !$tokenValidator->validate( $token ) ) {
|
||||
throw new RouterException('Invalid csrf-token.');
|
||||
if( !$this->csrfToken->validate( $token ) ) {
|
||||
throw new TokenMismatchException('Invalid csrf-token.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace Pecee\Http\Middleware;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
interface IMiddleware {
|
||||
public function handle(Request $request);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\Http\Middleware;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
|
||||
abstract class Middleware
|
||||
{
|
||||
abstract function handle(Request $request);
|
||||
}
|
||||
@@ -3,16 +3,31 @@ namespace Pecee\Http;
|
||||
|
||||
class Request {
|
||||
|
||||
protected static $instance;
|
||||
|
||||
protected $data;
|
||||
protected $uri;
|
||||
protected $host;
|
||||
protected $method;
|
||||
protected $headers;
|
||||
|
||||
/**
|
||||
* Return new instance
|
||||
* @return static
|
||||
*/
|
||||
public static function getInstance() {
|
||||
if(self::$instance === null) {
|
||||
self::$instance = new static();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function __construct() {
|
||||
$this->data = array();
|
||||
$this->host = $_SERVER['HTTP_HOST'];
|
||||
$this->uri = rtrim($_SERVER['REQUEST_URI'], '/') . '/';
|
||||
$this->uri = $_SERVER['REQUEST_URI'];
|
||||
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : strtolower($_SERVER['REQUEST_METHOD']);
|
||||
$this->headers = getallheaders();
|
||||
$this->headers = array_change_key_case(getallheaders(), CASE_LOWER);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,13 +75,55 @@ class Request {
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get id address
|
||||
* @return string
|
||||
*/
|
||||
public function getIp() {
|
||||
return isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get referer
|
||||
* @return string
|
||||
*/
|
||||
public function getReferer() {
|
||||
return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user agent
|
||||
* @return string
|
||||
*/
|
||||
public function getUserAgent() {
|
||||
return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get header value by name
|
||||
* @param string $name
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHeader($name) {
|
||||
return (isset($this->headers[$name])) ? $this->headers[$name] : null;
|
||||
return (isset($this->headers[strtolower($name)])) ? $this->headers[strtolower($name)] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request input or default value
|
||||
* @param string $name
|
||||
* @param string $defaultValue
|
||||
* @return mixed
|
||||
*/
|
||||
public function getInput($name, $defaultValue) {
|
||||
return (isset($_REQUEST[$name]) ? $_REQUEST[$name] : $defaultValue);
|
||||
}
|
||||
|
||||
public function __set($name, $value = null) {
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
|
||||
public function __get($name) {
|
||||
return isset($this->data[$name]) ? $this->data[$name] : null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,7 +21,7 @@ class Response {
|
||||
* @param string $url
|
||||
*/
|
||||
public function redirect($url) {
|
||||
header('location: ' . $url);
|
||||
$this->header('Location: ' . $url);
|
||||
die();
|
||||
}
|
||||
|
||||
@@ -29,9 +29,58 @@ class Response {
|
||||
$this->redirect(url());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add http authorisation
|
||||
* @param string $name
|
||||
* @return self $this
|
||||
*/
|
||||
public function auth($name = '') {
|
||||
header('WWW-Authenticate: Basic realm="' . $name . '"');
|
||||
header('HTTP/1.0 401 Unauthorized');
|
||||
$this->headers([
|
||||
'WWW-Authenticate: Basic realm="' . $name . '"',
|
||||
'HTTP/1.0 401 Unauthorized'
|
||||
]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function cache($duration = 2592000) {
|
||||
$this->headers([
|
||||
'Cache-Control: public,max-age='.$duration.',must-revalidate',
|
||||
'Expires: '.gmdate('D, d M Y H:i:s',(time()+$duration)).' GMT',
|
||||
'Last-modified: '.gmdate('D, d M Y H:i:s',time()).' GMT'
|
||||
]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Json encode array
|
||||
* @param array $value
|
||||
*/
|
||||
public function json(array $value) {
|
||||
$this->header('Content-type: application/json');
|
||||
echo json_encode($value);
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add header to response
|
||||
* @param string $value
|
||||
* @return self $this
|
||||
*/
|
||||
public function header($value) {
|
||||
header($value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add multiple headers to response
|
||||
* @param array $headers
|
||||
* @return self $this
|
||||
*/
|
||||
public function headers(array $headers) {
|
||||
foreach($headers as $header) {
|
||||
header($header);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,7 +27,7 @@ class RouterBase {
|
||||
$this->routes = array();
|
||||
$this->backstack = array();
|
||||
$this->controllerUrlMap = array();
|
||||
$this->request = new Request();
|
||||
$this->request = Request::getInstance();
|
||||
$this->baseCsrfVerifier = new BaseCsrfVerifier();
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ class RouterBase {
|
||||
|
||||
$newPrefixes = $prefixes;
|
||||
$mergedSettings = array_merge($settings, $route->getMergeableSettings());
|
||||
|
||||
if($route->getPrefix()) {
|
||||
array_push($newPrefixes, rtrim($route->getPrefix(), '/'));
|
||||
}
|
||||
@@ -100,11 +101,6 @@ class RouterBase {
|
||||
// Loop through each route-request
|
||||
$this->processRoutes($this->routes);
|
||||
|
||||
// Make sure the urls is in the right order when comparing
|
||||
usort($this->controllerUrlMap, function($a, $b) {
|
||||
return strcmp($b->getUrl(), $a->getUrl());
|
||||
});
|
||||
|
||||
$routeNotAllowed = false;
|
||||
|
||||
/* @var $route RouterEntry */
|
||||
@@ -121,6 +117,7 @@ class RouterBase {
|
||||
$routeNotAllowed = false;
|
||||
|
||||
$this->loadedRoute = $routeMatch;
|
||||
$routeMatch->loadMiddleware($this->request);
|
||||
$routeMatch->renderRoute($this->request);
|
||||
break;
|
||||
}
|
||||
@@ -230,6 +227,11 @@ class RouterBase {
|
||||
$url = str_ireplace('{' . $param. '}', $value, $url);
|
||||
$i++;
|
||||
}
|
||||
} else {
|
||||
// If no parameters are specified in the route, assume that the provided parameters should be used.
|
||||
if(count($parameters)) {
|
||||
$url = rtrim($url, '/') . '/' . join('/', $parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,7 +254,7 @@ class RouterBase {
|
||||
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
|
||||
}
|
||||
|
||||
if($controller === null && $parameters === null) {
|
||||
if($controller === null && $parameters === null && $this->loadedRoute !== null) {
|
||||
return $this->processUrl($this->loadedRoute, null, $getParams);
|
||||
}
|
||||
|
||||
@@ -306,7 +308,13 @@ class RouterBase {
|
||||
ArrayUtil::append($url, $parameters);
|
||||
}
|
||||
|
||||
return '/' . join('/', $url);
|
||||
$url = '/' . trim(join('/', $url), '/') . '/';
|
||||
|
||||
if(is_array($getParams)) {
|
||||
$url .= '?' . Url::arrayToParams($getParams);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
|
||||
@@ -17,6 +17,29 @@ class RouterController extends RouterEntry {
|
||||
$this->controller = $controller;
|
||||
}
|
||||
|
||||
public function renderRoute(Request $request) {
|
||||
if(is_object($this->getCallback()) && is_callable($this->getCallback())) {
|
||||
|
||||
// When the callback is a function
|
||||
call_user_func_array($this->getCallback(), $this->getParameters());
|
||||
} else {
|
||||
// When the callback is a method
|
||||
$controller = explode('@', $this->getCallback());
|
||||
$className = $this->getNamespace() . '\\' . $controller[0];
|
||||
|
||||
$class = $this->loadClass($className);
|
||||
$method = $request->getMethod() . ucfirst($controller[1]);
|
||||
|
||||
if (!method_exists($class, $method)) {
|
||||
throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||
}
|
||||
|
||||
call_user_func_array(array($class, $method), $this->getParameters());
|
||||
|
||||
return $class;
|
||||
}
|
||||
}
|
||||
|
||||
public function matchRoute(Request $request) {
|
||||
$url = parse_url($request->getUri());
|
||||
$url = rtrim($url['path'], '/') . '/';
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
use Pecee\Http\Middleware\Middleware;
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
|
||||
abstract class RouterEntry {
|
||||
@@ -10,26 +10,26 @@ abstract class RouterEntry {
|
||||
const REQUEST_TYPE_POST = 'post';
|
||||
const REQUEST_TYPE_GET = 'get';
|
||||
const REQUEST_TYPE_PUT = 'put';
|
||||
const REQUEST_TYPE_PATCH = 'patch';
|
||||
const REQUEST_TYPE_DELETE = 'delete';
|
||||
|
||||
public static $allowedRequestTypes = array(
|
||||
self::REQUEST_TYPE_DELETE,
|
||||
self::REQUEST_TYPE_GET,
|
||||
self::REQUEST_TYPE_POST,
|
||||
self::REQUEST_TYPE_PUT
|
||||
self::REQUEST_TYPE_PUT,
|
||||
self::REQUEST_TYPE_PATCH
|
||||
);
|
||||
|
||||
protected $settings;
|
||||
protected $callback;
|
||||
protected $parameters;
|
||||
protected $parametersRegex;
|
||||
protected $regexMatch;
|
||||
|
||||
public function __construct() {
|
||||
$this->settings = array();
|
||||
$this->settings['requestMethods'] = array();
|
||||
$this->settings['parametersRegex'] = array();
|
||||
$this->parameters = array();
|
||||
$this->parametersRegex = array();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,9 +179,9 @@ abstract class RouterEntry {
|
||||
public function getMergeableSettings() {
|
||||
$settings = $this->settings;
|
||||
|
||||
if(isset($settings['middleware'])) {
|
||||
/*if(isset($settings['middleware'])) {
|
||||
unset($settings['middleware']);
|
||||
}
|
||||
}*/
|
||||
|
||||
if(isset($settings['prefix'])) {
|
||||
unset($settings['prefix']);
|
||||
@@ -208,7 +208,7 @@ abstract class RouterEntry {
|
||||
public function setSettings($settings) {
|
||||
$this->settings = $settings;
|
||||
|
||||
if($settings['prefix']) {
|
||||
if(isset($settings['prefix'])) {
|
||||
$this->setPrefix($settings['prefix']);
|
||||
}
|
||||
|
||||
@@ -243,21 +243,31 @@ abstract class RouterEntry {
|
||||
return new $name();
|
||||
}
|
||||
|
||||
protected function loadMiddleware(Request $request) {
|
||||
public function loadMiddleware(Request $request) {
|
||||
if($this->getMiddleware()) {
|
||||
$middleware = $this->loadClass($this->getMiddleware());
|
||||
if (!($middleware instanceof Middleware)) {
|
||||
throw new RouterException($this->getMiddleware() . ' must be instance of Middleware');
|
||||
}
|
||||
if(is_array($this->getMiddleware())) {
|
||||
foreach($this->getMiddleware() as $middleware) {
|
||||
$middleware = $this->loadClass($middleware);
|
||||
if (!($middleware instanceof IMiddleware)) {
|
||||
throw new RouterException($middleware . ' must be instance of Middleware');
|
||||
}
|
||||
|
||||
/* @var $class Middleware */
|
||||
$middleware->handle($request);
|
||||
/* @var $class Middleware */
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function renderRoute(Request $request) {
|
||||
// Load middleware
|
||||
$this->loadMiddleware($request);
|
||||
|
||||
if(is_object($this->getCallback()) && is_callable($this->getCallback())) {
|
||||
|
||||
|
||||
@@ -10,10 +10,10 @@ class RouterGroup extends RouterEntry {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function matchRoute(Request $request) {
|
||||
public function renderRoute(Request $request) {
|
||||
// Check if request method is allowed
|
||||
|
||||
if(strtolower($request->getUri()) == strtolower($this->prefix) || stripos($request->getUri(), $this->prefix) === 0) {
|
||||
if(trim($this->prefix) === '' || strtolower($request->getUri()) == strtolower($this->prefix) || stripos($request->getUri(), $this->prefix) === 0) {
|
||||
|
||||
$hasAccess = (!$this->method);
|
||||
|
||||
@@ -29,11 +29,15 @@ class RouterGroup extends RouterEntry {
|
||||
throw new RouterException('Method not allowed');
|
||||
}
|
||||
|
||||
return $this;
|
||||
return parent::renderRoute($request);
|
||||
}
|
||||
|
||||
// No match here, move on...
|
||||
return null;
|
||||
}
|
||||
|
||||
public function matchRoute(Request $request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,9 +20,6 @@ class RouterResource extends RouterEntry {
|
||||
}
|
||||
|
||||
public function renderRoute(Request $request) {
|
||||
// Load middleware
|
||||
$this->loadMiddleware($request);
|
||||
|
||||
if(is_object($this->getCallback()) && is_callable($this->getCallback())) {
|
||||
// When the callback is a function
|
||||
call_user_func_array($this->getCallback(), $this->getParameters());
|
||||
|
||||
@@ -19,6 +19,7 @@ class RouterRoute extends RouterEntry {
|
||||
}
|
||||
|
||||
protected function parseParameters($url, $multiple = false, $regex = self::PARAMETERS_REGEX_MATCH) {
|
||||
$url = rtrim($url, '/');
|
||||
$parameters = array();
|
||||
|
||||
if($multiple) {
|
||||
@@ -60,8 +61,10 @@ class RouterRoute extends RouterEntry {
|
||||
|
||||
} else {
|
||||
|
||||
$matches = (count(explode('/', rtrim($url, '/'))) == count(explode('/', rtrim($route, '/'))));
|
||||
|
||||
$url = explode('/', $url);
|
||||
$route = explode('/', $route);
|
||||
$route = explode('/', rtrim($route, '/'));
|
||||
|
||||
$parameters = array();
|
||||
|
||||
@@ -90,8 +93,8 @@ class RouterRoute extends RouterEntry {
|
||||
}
|
||||
}
|
||||
|
||||
// Add parameter value
|
||||
$parameters[$parameter] = $parameterValue;
|
||||
// Add parameter value, if it doesn't exist - replace it with null value
|
||||
$parameters[$parameter] = ($parameterValue === '') ? null : $parameterValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,5 +163,4 @@ class RouterRoute extends RouterEntry {
|
||||
return parent::setSettings($settings);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -57,7 +57,7 @@ class SimpleRouter {
|
||||
public static function put($url, $callback, array $settings = null) {
|
||||
$route = new RouterRoute($url, $callback);
|
||||
$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->addRoute($route);
|
||||
@@ -128,7 +128,7 @@ class SimpleRouter {
|
||||
return $route;
|
||||
}
|
||||
|
||||
public function getRoute($controller = null, $parameters = null, $getParams = null) {
|
||||
public static function getRoute($controller = null, $parameters = null, $getParams = null) {
|
||||
return RouterBase::getInstance()->getRoute($controller, $parameters, $getParams);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user