mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 22:12:18 +00:00
@@ -600,7 +600,7 @@ SimpleRouter::group(['namespace' => 'Admin'], function () {
|
|||||||
|
|
||||||
### Sub domain-routing
|
### Sub domain-routing
|
||||||
|
|
||||||
Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using the `domain` key on the group attribute array:
|
Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route urls, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using the `domain` key on the group attribute array:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
SimpleRouter::group(['domain' => '{account}.myapp.com'], function () {
|
SimpleRouter::group(['domain' => '{account}.myapp.com'], function () {
|
||||||
@@ -612,7 +612,7 @@ SimpleRouter::group(['domain' => '{account}.myapp.com'], function () {
|
|||||||
|
|
||||||
### Route prefixes
|
### Route prefixes
|
||||||
|
|
||||||
The `prefix` group attribute may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with `admin`:
|
The `prefix` group attribute may be used to prefix each route in the group with a given url. For example, you may want to prefix all route urls within the group with `admin`:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
SimpleRouter::group(['prefix' => '/admin'], function () {
|
SimpleRouter::group(['prefix' => '/admin'], function () {
|
||||||
@@ -894,7 +894,7 @@ class CustomExceptionHandler implements IExceptionHandler
|
|||||||
|
|
||||||
/* You can use the exception handler to format errors depending on the request and type. */
|
/* You can use the exception handler to format errors depending on the request and type. */
|
||||||
|
|
||||||
if (stripos($request->getUri()->getPath(), '/api') !== false) {
|
if (stripos($request->getUrl()->getPath(), '/api') !== false) {
|
||||||
|
|
||||||
response()->json([
|
response()->json([
|
||||||
'error' => $error->getMessage(),
|
'error' => $error->getMessage(),
|
||||||
@@ -1171,9 +1171,9 @@ class CustomRouterRules implement IRouterBootManager {
|
|||||||
|
|
||||||
foreach($rewriteRules as $url => $rule) {
|
foreach($rewriteRules as $url => $rule) {
|
||||||
|
|
||||||
// If the current uri matches the url, we use our custom route
|
// If the current url matches the rewrite url, we use our custom route
|
||||||
|
|
||||||
if($request->getUri()->getPath() === $url) {
|
if($request->getUrl()->getPath() === $url) {
|
||||||
$request->setRewriteUrl($rule);
|
$request->setRewriteUrl($rule);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
namespace Pecee\Exceptions;
|
||||||
|
|
||||||
|
class InvalidArgumentException extends \InvalidArgumentException {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pecee\Http\Exceptions;
|
||||||
|
|
||||||
|
class MalformedUrlException extends \Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Pecee\Http\Input;
|
namespace Pecee\Http\Input;
|
||||||
|
|
||||||
|
use Pecee\Exceptions\InvalidArgumentException;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
|
||||||
class Input
|
class Input
|
||||||
@@ -26,6 +27,10 @@ class Input
|
|||||||
*/
|
*/
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Input constructor.
|
||||||
|
* @param Request $request
|
||||||
|
*/
|
||||||
public function __construct(Request $request)
|
public function __construct(Request $request)
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
@@ -33,6 +38,10 @@ class Input
|
|||||||
$this->parseInputs();
|
$this->parseInputs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse input values
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function parseInputs()
|
public function parseInputs()
|
||||||
{
|
{
|
||||||
/* Parse get requests */
|
/* Parse get requests */
|
||||||
@@ -57,6 +66,9 @@ class Input
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function parseFiles()
|
public function parseFiles()
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
@@ -66,7 +78,11 @@ class Input
|
|||||||
// Handle array input
|
// Handle array input
|
||||||
if (is_array($value['name']) === false) {
|
if (is_array($value['name']) === false) {
|
||||||
$values['index'] = $key;
|
$values['index'] = $key;
|
||||||
|
try {
|
||||||
$list[$key] = InputFile::createFromArray($values + $value);
|
$list[$key] = InputFile::createFromArray($values + $value);
|
||||||
|
} catch(InvalidArgumentException $e ){
|
||||||
|
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,6 +113,8 @@ class Input
|
|||||||
|
|
||||||
if (is_array($original['name'][$key]) === false) {
|
if (is_array($original['name'][$key]) === false) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
$file = InputFile::createFromArray([
|
$file = InputFile::createFromArray([
|
||||||
'index' => (empty($key) === true && empty($originalIndex) === false) ? $originalIndex : $key,
|
'index' => (empty($key) === true && empty($originalIndex) === false) ? $originalIndex : $key,
|
||||||
'name' => $original['name'][$key],
|
'name' => $original['name'][$key],
|
||||||
@@ -113,6 +131,10 @@ class Input
|
|||||||
|
|
||||||
$output[$key] = $file;
|
$output[$key] = $file;
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
} catch(InvalidArgumentException $e) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$index[] = $key;
|
$index[] = $key;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Pecee\Http\Input;
|
namespace Pecee\Http\Input;
|
||||||
|
|
||||||
|
use Pecee\Exceptions\InvalidArgumentException;
|
||||||
|
|
||||||
class InputFile implements IInputItem
|
class InputFile implements IInputItem
|
||||||
{
|
{
|
||||||
public $index;
|
public $index;
|
||||||
@@ -26,13 +28,13 @@ class InputFile implements IInputItem
|
|||||||
* Create from array
|
* Create from array
|
||||||
*
|
*
|
||||||
* @param array $values
|
* @param array $values
|
||||||
* @throws \InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public static function createFromArray(array $values)
|
public static function createFromArray(array $values)
|
||||||
{
|
{
|
||||||
if (isset($values['index']) === false) {
|
if (isset($values['index']) === false) {
|
||||||
throw new \InvalidArgumentException('Index key is required');
|
throw new InvalidArgumentException('Index key is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Easy way of ensuring that all indexes-are set and not filling the screen with isset() */
|
/* Easy way of ensuring that all indexes-are set and not filling the screen with isset() */
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ class BaseCsrfVerifier implements IMiddleware
|
|||||||
protected $except;
|
protected $except;
|
||||||
protected $tokenProvider;
|
protected $tokenProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BaseCsrfVerifier constructor.
|
||||||
|
* @throws \Pecee\Http\Security\Exceptions\SecurityException
|
||||||
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->tokenProvider = new CookieTokenProvider();
|
$this->tokenProvider = new CookieTokenProvider();
|
||||||
@@ -39,9 +43,9 @@ class BaseCsrfVerifier implements IMiddleware
|
|||||||
$url = rtrim($url, '/');
|
$url = rtrim($url, '/');
|
||||||
if ($url[strlen($url) - 1] === '*') {
|
if ($url[strlen($url) - 1] === '*') {
|
||||||
$url = rtrim($url, '*');
|
$url = rtrim($url, '*');
|
||||||
$skip = (stripos($request->getUri()->getPath(), $url) === 0);
|
$skip = (stripos($request->getUrl()->getOriginalUrl(), $url) === 0);
|
||||||
} else {
|
} else {
|
||||||
$skip = ($url === $request->getUri()->getPath());
|
$skip = ($url === $request->getUrl()->getOriginalUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($skip === true) {
|
if ($skip === true) {
|
||||||
|
|||||||
+24
-13
@@ -12,7 +12,7 @@ class Request
|
|||||||
private $data = [];
|
private $data = [];
|
||||||
protected $headers;
|
protected $headers;
|
||||||
protected $host;
|
protected $host;
|
||||||
protected $uri;
|
protected $url;
|
||||||
protected $method;
|
protected $method;
|
||||||
protected $input;
|
protected $input;
|
||||||
|
|
||||||
@@ -29,13 +29,17 @@ class Request
|
|||||||
*/
|
*/
|
||||||
protected $loadedRoute;
|
protected $loadedRoute;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request constructor.
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->parseHeaders();
|
$this->parseHeaders();
|
||||||
$this->setHost($this->getHeader('http-host'));
|
$this->setHost($this->getHeader('http-host'));
|
||||||
|
|
||||||
// Check if special IIS header exist, otherwise use default.
|
// Check if special IIS header exist, otherwise use default.
|
||||||
$this->setUri(new Uri($this->getHeader('unencoded-url', $this->getHeader('request-uri'))));
|
$this->setUrl($this->getHeader('unencoded-url', $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')));
|
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method')));
|
||||||
@@ -58,11 +62,11 @@ class Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Uri
|
* @return Url
|
||||||
*/
|
*/
|
||||||
public function getUri()
|
public function getUrl()
|
||||||
{
|
{
|
||||||
return $this->uri;
|
return $this->url;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -188,6 +192,16 @@ class Request
|
|||||||
return ($this->getHeader('http-accept') !== null && stripos($this->getHeader('http-accept'), $format) > -1);
|
return ($this->getHeader('http-accept') !== null && stripos($this->getHeader('http-accept'), $format) > -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the request is made through Ajax
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isAjax()
|
||||||
|
{
|
||||||
|
return (strtolower($this->getHeader('http-x-requested-with')) === 'xmlhttprequest');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get accept formats
|
* Get accept formats
|
||||||
* @return array
|
* @return array
|
||||||
@@ -198,15 +212,12 @@ class Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Uri|string $uri
|
* @param string|Url $url
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public function setUri($uri)
|
public function setUrl($url)
|
||||||
{
|
{
|
||||||
if (is_string($uri) === true) {
|
$this->url = is_string($url) ? new Url($url) : $url;
|
||||||
$uri = new Uri($uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->uri = $uri;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -282,7 +293,7 @@ class Request
|
|||||||
{
|
{
|
||||||
$this->hasRewrite = true;
|
$this->hasRewrite = true;
|
||||||
|
|
||||||
return $this->setRewriteRoute(new RouteUrl($this->getUri()->getPath(), $callback));
|
return $this->setRewriteRoute(new RouteUrl($this->getUrl()->getPath(), $callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Pecee\Http;
|
namespace Pecee\Http;
|
||||||
|
|
||||||
|
use Pecee\Exceptions\InvalidArgumentException;
|
||||||
|
|
||||||
class Response
|
class Response
|
||||||
{
|
{
|
||||||
protected $request;
|
protected $request;
|
||||||
@@ -42,7 +44,7 @@ class Response
|
|||||||
|
|
||||||
public function refresh()
|
public function refresh()
|
||||||
{
|
{
|
||||||
$this->redirect($this->request->getUri()->getOriginalUrl());
|
$this->redirect($this->request->getUrl()->getOriginalUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,12 +89,12 @@ class Response
|
|||||||
* @param array|\JsonSerializable $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 $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.
|
* @param int $dept JSON debt.
|
||||||
* @throws \InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function json($value, $options = null, $dept = 512)
|
public function json($value, $options = null, $dept = 512)
|
||||||
{
|
{
|
||||||
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
||||||
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
throw new InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->header('Content-Type: application/json; charset=utf-8');
|
$this->header('Content-Type: application/json; charset=utf-8');
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Pecee\Http\Security;
|
namespace Pecee\Http\Security;
|
||||||
|
|
||||||
|
use Pecee\Http\Security\Exceptions\SecurityException;
|
||||||
|
|
||||||
class CookieTokenProvider implements ITokenProvider
|
class CookieTokenProvider implements ITokenProvider
|
||||||
{
|
{
|
||||||
const CSRF_KEY = 'CSRF-TOKEN';
|
const CSRF_KEY = 'CSRF-TOKEN';
|
||||||
@@ -9,6 +11,10 @@ class CookieTokenProvider implements ITokenProvider
|
|||||||
protected $token;
|
protected $token;
|
||||||
protected $cookieTimeoutMinutes = 120;
|
protected $cookieTimeoutMinutes = 120;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CookieTokenProvider constructor.
|
||||||
|
* @throws SecurityException
|
||||||
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->token = $this->getToken();
|
$this->token = $this->getToken();
|
||||||
@@ -21,20 +27,24 @@ class CookieTokenProvider implements ITokenProvider
|
|||||||
/**
|
/**
|
||||||
* Generate random identifier for CSRF token
|
* Generate random identifier for CSRF token
|
||||||
*
|
*
|
||||||
* @throws \RuntimeException|\Exception
|
|
||||||
* @return string
|
* @return string
|
||||||
|
* @throws SecurityException
|
||||||
*/
|
*/
|
||||||
public function generateToken()
|
public function generateToken()
|
||||||
{
|
{
|
||||||
if (function_exists('random_bytes') === true) {
|
if (function_exists('random_bytes') === true) {
|
||||||
|
try {
|
||||||
return bin2hex(random_bytes(32));
|
return bin2hex(random_bytes(32));
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
throw new SecurityException($e->getMessage(), $e->getCode(), $e->getPrevious());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$isSourceStrong = false;
|
$isSourceStrong = false;
|
||||||
|
|
||||||
$random = openssl_random_pseudo_bytes(32, $isSourceStrong);
|
$random = openssl_random_pseudo_bytes(32, $isSourceStrong);
|
||||||
if ($isSourceStrong === false || $random === false) {
|
if ($isSourceStrong === false || $random === false) {
|
||||||
throw new \RuntimeException('IV generation failed');
|
throw new SecurityException('IV generation failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $random;
|
return $random;
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
namespace Pecee\Http\Security\Exceptions;
|
||||||
|
|
||||||
|
class SecurityException extends \Exception {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
namespace Pecee\Http;
|
namespace Pecee\Http;
|
||||||
|
|
||||||
class Uri
|
use Pecee\Http\Exceptions\MalformedUrlException;
|
||||||
|
|
||||||
|
class Url
|
||||||
{
|
{
|
||||||
private $originalUrl;
|
private $originalUrl;
|
||||||
private $data = [
|
private $data = [
|
||||||
@@ -16,6 +18,11 @@ class Uri
|
|||||||
'fragment' => null,
|
'fragment' => null,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Url constructor.
|
||||||
|
* @param string $url
|
||||||
|
* @throws MalformedUrlException
|
||||||
|
*/
|
||||||
public function __construct($url)
|
public function __construct($url)
|
||||||
{
|
{
|
||||||
$this->originalUrl = $url;
|
$this->originalUrl = $url;
|
||||||
@@ -129,7 +136,7 @@ class Uri
|
|||||||
* UTF-8 aware parse_url() replacement.
|
* UTF-8 aware parse_url() replacement.
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param int $component
|
* @param int $component
|
||||||
* @throws \InvalidArgumentException
|
* @throws MalformedUrlException
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function parseUrl($url, $component = -1)
|
public function parseUrl($url, $component = -1)
|
||||||
@@ -145,7 +152,7 @@ class Uri
|
|||||||
$parts = parse_url($encodedUrl, $component);
|
$parts = parse_url($encodedUrl, $component);
|
||||||
|
|
||||||
if ($parts === false) {
|
if ($parts === false) {
|
||||||
throw new \InvalidArgumentException('Malformed URL: ' . $url);
|
throw new MalformedUrlException('Malformed URL: ' . $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_map('urldecode', $parts);
|
return array_map('urldecode', $parts);
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
|
use Pecee\Exceptions\InvalidArgumentException;
|
||||||
use Pecee\Handlers\IExceptionHandler;
|
use Pecee\Handlers\IExceptionHandler;
|
||||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
@@ -65,11 +66,18 @@ class Router
|
|||||||
*/
|
*/
|
||||||
protected $exceptionHandlers;
|
protected $exceptionHandlers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Router constructor.
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->reset();
|
$this->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
|
*/
|
||||||
public function reset()
|
public function reset()
|
||||||
{
|
{
|
||||||
$this->processingRoute = false;
|
$this->processingRoute = false;
|
||||||
@@ -116,7 +124,7 @@ class Router
|
|||||||
|
|
||||||
$exceptionHandlers = [];
|
$exceptionHandlers = [];
|
||||||
|
|
||||||
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
|
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUrl()->getPath();
|
||||||
|
|
||||||
foreach ($routes as $route) {
|
foreach ($routes as $route) {
|
||||||
|
|
||||||
@@ -208,7 +216,6 @@ class Router
|
|||||||
* @param bool $rewrite
|
* @param bool $rewrite
|
||||||
* @return string|mixed
|
* @return string|mixed
|
||||||
* @throws HttpException
|
* @throws HttpException
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function routeRequest($rewrite = false)
|
public function routeRequest($rewrite = false)
|
||||||
{
|
{
|
||||||
@@ -228,7 +235,7 @@ class Router
|
|||||||
$this->request->setHasRewrite(false);
|
$this->request->setHasRewrite(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
|
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUrl()->getPath();
|
||||||
|
|
||||||
/* @var $route ILoadableRoute */
|
/* @var $route ILoadableRoute */
|
||||||
foreach ($this->processedRoutes as $key => $route) {
|
foreach ($this->processedRoutes as $key => $route) {
|
||||||
@@ -274,7 +281,7 @@ class Router
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($routeNotAllowed === true) {
|
if ($routeNotAllowed === true) {
|
||||||
$message = sprintf('Route "%s" or method "%s" not allowed.', $this->request->getUri()->getPath(), $this->request->getMethod());
|
$message = sprintf('Route "%s" or method "%s" not allowed.', $this->request->getUrl()->getPath(), $this->request->getMethod());
|
||||||
$this->handleException(new HttpException($message, 403));
|
$this->handleException(new HttpException($message, 403));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,9 +290,9 @@ class Router
|
|||||||
$rewriteUrl = $this->request->getRewriteUrl();
|
$rewriteUrl = $this->request->getRewriteUrl();
|
||||||
|
|
||||||
if ($rewriteUrl !== null) {
|
if ($rewriteUrl !== null) {
|
||||||
$message = sprintf('Route not found: "%s" (rewrite from: "%s")', $rewriteUrl, $this->request->getUri()->getPath());
|
$message = sprintf('Route not found: "%s" (rewrite from: "%s")', $rewriteUrl, $this->request->getUrl()->getPath());
|
||||||
} else {
|
} else {
|
||||||
$message = sprintf('Route not found: "%s"', $this->request->getUri()->getPath());
|
$message = sprintf('Route not found: "%s"', $this->request->getUrl()->getPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->handleException(new NotFoundHttpException($message, 404));
|
$this->handleException(new NotFoundHttpException($message, 404));
|
||||||
@@ -322,7 +329,6 @@ class Router
|
|||||||
/**
|
/**
|
||||||
* @param \Exception $e
|
* @param \Exception $e
|
||||||
* @throws HttpException
|
* @throws HttpException
|
||||||
* @throws \Exception
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function handleException(\Exception $e)
|
protected function handleException(\Exception $e)
|
||||||
@@ -354,7 +360,7 @@ class Router
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw $e;
|
throw new HttpException($e->getMessage(), $e->getCode(), $e->getPrevious());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function arrayToParams(array $getParams = [], $includeEmpty = true)
|
public function arrayToParams(array $getParams = [], $includeEmpty = true)
|
||||||
@@ -436,13 +442,13 @@ class Router
|
|||||||
* @param string|null $name
|
* @param string|null $name
|
||||||
* @param string|array|null $parameters
|
* @param string|array|null $parameters
|
||||||
* @param array|null $getParams
|
* @param array|null $getParams
|
||||||
* @throws \InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getUrl($name = null, $parameters = null, $getParams = null)
|
public function getUrl($name = null, $parameters = null, $getParams = null)
|
||||||
{
|
{
|
||||||
if ($getParams !== null && is_array($getParams) === false) {
|
if ($getParams !== null && is_array($getParams) === false) {
|
||||||
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
|
throw new InvalidArgumentException('Invalid type for getParams. Must be array or null');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($name === '' && $parameters === '') {
|
if ($name === '' && $parameters === '') {
|
||||||
@@ -458,7 +464,7 @@ class Router
|
|||||||
|
|
||||||
/* Return current route if no options has been specified */
|
/* Return current route if no options has been specified */
|
||||||
if ($name === null && $parameters === null) {
|
if ($name === null && $parameters === null) {
|
||||||
return $this->request->getUri()->getPath() . $this->arrayToParams($getParams);
|
return $this->request->getUrl()->getPath() . $this->arrayToParams($getParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
$loadedRoute = $this->request->getLoadedRoute();
|
$loadedRoute = $this->request->getLoadedRoute();
|
||||||
|
|||||||
@@ -10,11 +10,11 @@
|
|||||||
|
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
|
use Pecee\Exceptions\InvalidArgumentException;
|
||||||
use Pecee\Handlers\CallbackExceptionHandler;
|
use Pecee\Handlers\CallbackExceptionHandler;
|
||||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||||
use Pecee\Http\Response;
|
use Pecee\Http\Response;
|
||||||
use Pecee\SimpleRouter\Exceptions\HttpException;
|
use Pecee\SimpleRouter\Exceptions\HttpException;
|
||||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
|
||||||
use Pecee\SimpleRouter\Route\IRoute;
|
use Pecee\SimpleRouter\Route\IRoute;
|
||||||
use Pecee\SimpleRouter\Route\RoutePartialGroup;
|
use Pecee\SimpleRouter\Route\RoutePartialGroup;
|
||||||
use Pecee\SimpleRouter\Route\RouteController;
|
use Pecee\SimpleRouter\Route\RouteController;
|
||||||
@@ -43,9 +43,8 @@ class SimpleRouter
|
|||||||
protected static $router;
|
protected static $router;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start/route request
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*
|
* @throws HttpException
|
||||||
* @throws HttpException|NotFoundHttpException|\Exception
|
|
||||||
*/
|
*/
|
||||||
public static function start()
|
public static function start()
|
||||||
{
|
{
|
||||||
@@ -66,6 +65,7 @@ class SimpleRouter
|
|||||||
* Base CSRF verifier
|
* Base CSRF verifier
|
||||||
*
|
*
|
||||||
* @param BaseCsrfVerifier $baseCsrfVerifier
|
* @param BaseCsrfVerifier $baseCsrfVerifier
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function csrfVerifier(BaseCsrfVerifier $baseCsrfVerifier)
|
public static function csrfVerifier(BaseCsrfVerifier $baseCsrfVerifier)
|
||||||
{
|
{
|
||||||
@@ -77,6 +77,7 @@ class SimpleRouter
|
|||||||
* Perfect if you want to load pretty-urls from a file or database.
|
* Perfect if you want to load pretty-urls from a file or database.
|
||||||
*
|
*
|
||||||
* @param IRouterBootManager $bootManager
|
* @param IRouterBootManager $bootManager
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function addBootManager(IRouterBootManager $bootManager)
|
public static function addBootManager(IRouterBootManager $bootManager)
|
||||||
{
|
{
|
||||||
@@ -89,7 +90,9 @@ class SimpleRouter
|
|||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
|
*
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function get($url, $callback, array $settings = null)
|
public static function get($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -103,6 +106,7 @@ class SimpleRouter
|
|||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function post($url, $callback, array $settings = null)
|
public static function post($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -116,6 +120,7 @@ class SimpleRouter
|
|||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function put($url, $callback, array $settings = null)
|
public static function put($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -129,6 +134,7 @@ class SimpleRouter
|
|||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function patch($url, $callback, array $settings = null)
|
public static function patch($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -142,6 +148,7 @@ class SimpleRouter
|
|||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function options($url, $callback, array $settings = null)
|
public static function options($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -155,6 +162,7 @@ class SimpleRouter
|
|||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function delete($url, $callback, array $settings = null)
|
public static function delete($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -166,13 +174,14 @@ class SimpleRouter
|
|||||||
*
|
*
|
||||||
* @param array $settings
|
* @param array $settings
|
||||||
* @param \Closure $callback
|
* @param \Closure $callback
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
* @return RouteGroup
|
* @return RouteGroup
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public static function group(array $settings = [], \Closure $callback)
|
public static function group(array $settings = [], \Closure $callback)
|
||||||
{
|
{
|
||||||
if (is_callable($callback) === false) {
|
if (is_callable($callback) === false) {
|
||||||
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
throw new InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
$group = new RouteGroup();
|
$group = new RouteGroup();
|
||||||
@@ -189,15 +198,16 @@ class SimpleRouter
|
|||||||
* parameters and which are only rendered when the url matches.
|
* parameters and which are only rendered when the url matches.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param array $settings
|
|
||||||
* @param \Closure $callback
|
* @param \Closure $callback
|
||||||
* @throws \InvalidArgumentException
|
* @param array $settings
|
||||||
* @return RoutePartialGroup
|
* @return RoutePartialGroup
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public static function partialGroup($url, \Closure $callback, array $settings = [])
|
public static function partialGroup($url, \Closure $callback, array $settings = [])
|
||||||
{
|
{
|
||||||
if (is_callable($callback) === false) {
|
if (is_callable($callback) === false) {
|
||||||
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
throw new InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
$settings['prefix'] = $url;
|
$settings['prefix'] = $url;
|
||||||
@@ -219,6 +229,7 @@ class SimpleRouter
|
|||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @see SimpleRouter::form
|
* @see SimpleRouter::form
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function basic($url, $callback, array $settings = null)
|
public static function basic($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -234,6 +245,7 @@ class SimpleRouter
|
|||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @see SimpleRouter::form
|
* @see SimpleRouter::form
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function form($url, $callback, array $settings = null)
|
public static function form($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -248,6 +260,7 @@ class SimpleRouter
|
|||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl|IRoute
|
* @return RouteUrl|IRoute
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function match(array $requestMethods, $url, $callback, array $settings = null)
|
public static function match(array $requestMethods, $url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -271,6 +284,7 @@ class SimpleRouter
|
|||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl|IRoute
|
* @return RouteUrl|IRoute
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function all($url, $callback, array $settings = null)
|
public static function all($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -293,6 +307,7 @@ class SimpleRouter
|
|||||||
* @param string $controller
|
* @param string $controller
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteController|IRoute
|
* @return RouteController|IRoute
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function controller($url, $controller, array $settings = null)
|
public static function controller($url, $controller, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -315,6 +330,7 @@ class SimpleRouter
|
|||||||
* @param string $controller
|
* @param string $controller
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteResource|IRoute
|
* @return RouteResource|IRoute
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function resource($url, $controller, array $settings = null)
|
public static function resource($url, $controller, array $settings = null)
|
||||||
{
|
{
|
||||||
@@ -335,6 +351,7 @@ class SimpleRouter
|
|||||||
*
|
*
|
||||||
* @param \Closure $callback
|
* @param \Closure $callback
|
||||||
* @return CallbackExceptionHandler $callbackHandler
|
* @return CallbackExceptionHandler $callbackHandler
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function error(\Closure $callback)
|
public static function error(\Closure $callback)
|
||||||
{
|
{
|
||||||
@@ -367,8 +384,9 @@ class SimpleRouter
|
|||||||
* @param string|null $name
|
* @param string|null $name
|
||||||
* @param string|array|null $parameters
|
* @param string|array|null $parameters
|
||||||
* @param array|null $getParams
|
* @param array|null $getParams
|
||||||
* @throws \InvalidArgumentException
|
* @throws \Pecee\Exceptions\InvalidArgumentException
|
||||||
* @return string
|
* @return string
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function getUrl($name = null, $parameters = null, $getParams = null)
|
public static function getUrl($name = null, $parameters = null, $getParams = null)
|
||||||
{
|
{
|
||||||
@@ -379,6 +397,7 @@ class SimpleRouter
|
|||||||
* Get the request
|
* Get the request
|
||||||
*
|
*
|
||||||
* @return \Pecee\Http\Request
|
* @return \Pecee\Http\Request
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function request()
|
public static function request()
|
||||||
{
|
{
|
||||||
@@ -389,6 +408,7 @@ class SimpleRouter
|
|||||||
* Get the response object
|
* Get the response object
|
||||||
*
|
*
|
||||||
* @return Response
|
* @return Response
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function response()
|
public static function response()
|
||||||
{
|
{
|
||||||
@@ -403,6 +423,7 @@ class SimpleRouter
|
|||||||
* Returns the router instance
|
* Returns the router instance
|
||||||
*
|
*
|
||||||
* @return Router
|
* @return Router
|
||||||
|
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||||
*/
|
*/
|
||||||
public static function router()
|
public static function router()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class ExceptionHandlerFirst implements \Pecee\Handlers\IExceptionHandler
|
|||||||
global $stack;
|
global $stack;
|
||||||
$stack[] = static::class;
|
$stack[] = static::class;
|
||||||
|
|
||||||
$request->setUri(new \Pecee\Http\Uri('/'));
|
$request->setUrl('/');
|
||||||
return $request;
|
return $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class ExceptionHandlerSecond implements \Pecee\Handlers\IExceptionHandler
|
|||||||
global $stack;
|
global $stack;
|
||||||
$stack[] = static::class;
|
$stack[] = static::class;
|
||||||
|
|
||||||
$request->setUri(new \Pecee\Http\Uri('/'));
|
$request->setUrl('/');
|
||||||
return $request;
|
return $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,18 +3,18 @@
|
|||||||
class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
|
class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
|
||||||
{
|
{
|
||||||
|
|
||||||
public static function debugNoReset($testUri, $testMethod = 'get')
|
public static function debugNoReset($testUrl, $testMethod = 'get')
|
||||||
{
|
{
|
||||||
static::request()->setUri(new \Pecee\Http\Uri($testUri));
|
static::request()->setUrl($testUrl);
|
||||||
static::request()->setMethod($testMethod);
|
static::request()->setMethod($testMethod);
|
||||||
|
|
||||||
static::start();
|
static::start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function debug($testUri, $testMethod = 'get')
|
public static function debug($testUrl, $testMethod = 'get')
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
static::debugNoReset($testUri, $testMethod);
|
static::debugNoReset($testUrl, $testMethod);
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
static::router()->reset();
|
static::router()->reset();
|
||||||
throw $e;
|
throw $e;
|
||||||
@@ -24,13 +24,13 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function debugOutput($testUri, $testMethod = 'get')
|
public static function debugOutput($testUrl, $testMethod = 'get')
|
||||||
{
|
{
|
||||||
$response = null;
|
$response = null;
|
||||||
|
|
||||||
// Route request
|
// Route request
|
||||||
ob_start();
|
ob_start();
|
||||||
static::debug($testUri, $testMethod);
|
static::debug($testUrl, $testMethod);
|
||||||
$response = ob_get_contents();
|
$response = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ class RouteRewriteTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
TestRouter::error(function (\Pecee\Http\Request $request, \Exception $error) {
|
TestRouter::error(function (\Pecee\Http\Request $request, \Exception $error) {
|
||||||
|
|
||||||
if (strtolower($request->getUri()->getPath()) === '/my/test/') {
|
if (strtolower($request->getUrl()->getPath()) === '/my/test/') {
|
||||||
$request->setRewriteUrl('/another-non-existing');
|
$request->setRewriteUrl('/another-non-existing');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user