mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 16:12:14 +00:00
[FEATURE]
- Added support for custom ExceptionHandlers on group level. - Routes now contain parent group, if any. - Fixed wrong usage of required parameter.
This commit is contained in:
@@ -72,7 +72,7 @@ use Pecee\SimpleRouter\SimpleRouter;
|
|||||||
|
|
||||||
SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\SomeMiddlewareClass'], function() {
|
SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\SomeMiddlewareClass'], function() {
|
||||||
|
|
||||||
SimpleRouter::group(['prefix' => 'services'], function() {
|
SimpleRouter::group(['prefix' => '/services', 'exceptionHandler' => '\MyProject\Handler\CustomExceptionHandler'], function() {
|
||||||
|
|
||||||
SimpleRouter::get('/answers/{id}', 'ControllerAnswers@show')->where(['id' => '[0-9]+');
|
SimpleRouter::get('/answers/{id}', 'ControllerAnswers@show')->where(['id' => '[0-9]+');
|
||||||
|
|
||||||
@@ -131,47 +131,57 @@ The framework has it's own ```Router``` class which inherits from the ```SimpleR
|
|||||||
namespace MyProject;
|
namespace MyProject;
|
||||||
|
|
||||||
use Pecee\Handler\ExceptionHandler;
|
use Pecee\Handler\ExceptionHandler;
|
||||||
|
use Pecee\SimpleRouter\RouterBase;
|
||||||
use Pecee\SimpleRouter\SimpleRouter;
|
use Pecee\SimpleRouter\SimpleRouter;
|
||||||
|
|
||||||
class Router extends SimpleRouter {
|
class Router extends SimpleRouter {
|
||||||
|
|
||||||
protected static $exceptionHandlers = array();
|
protected static $defaultExceptionHandler;
|
||||||
|
|
||||||
public static function start() {
|
public static function start($defaultNamespace = null) {
|
||||||
|
|
||||||
Debug::getInstance()->add('Router initialised.');
|
|
||||||
|
|
||||||
// Load routes.php
|
// Load routes.php
|
||||||
$file = $_ENV['basePath'] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
|
$file = $_ENV['base_path'] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
|
||||||
if(file_exists($file)) {
|
if(file_exists($file)) {
|
||||||
require_once $file;
|
require_once $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init locale settings
|
// Set default namespace
|
||||||
Locale::getInstance();
|
$defaultNamespace = '\\'.$_ENV['app_name'] . '\\Controller';
|
||||||
|
|
||||||
// Set default namespace for routes
|
|
||||||
$defaultNamespace = '\\'.Registry::getInstance()->get('AppName') . '\\Controller';
|
|
||||||
|
|
||||||
// Add custom csrf verifier (must extend BaseCsrfVerifier)
|
|
||||||
parent::csrfVerifier('MyProject\Middleware\CustomCsrfVerifier');
|
|
||||||
|
|
||||||
// Handle exceptions
|
// Handle exceptions
|
||||||
try {
|
try {
|
||||||
parent::start($defaultNamespace);
|
parent::start($defaultNamespace);
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
/* @var $handler ExceptionHandler */
|
|
||||||
foreach(self::$exceptionHandlers as $handler) {
|
$route = RouterBase::getInstance()->getLoadedRoute();
|
||||||
$class = new $handler();
|
|
||||||
$class->handleError($e);
|
$exceptionHandler = null;
|
||||||
|
|
||||||
|
// Load and use exception-handler defined on group
|
||||||
|
|
||||||
|
if($route && $route->getGroup()) {
|
||||||
|
$exceptionHandler = $route->getGroup()->getExceptionHandler();
|
||||||
|
|
||||||
|
if($exceptionHandler !== null) {
|
||||||
|
$class = new $exceptionHandler();
|
||||||
|
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise use the fallback default exceptions handler
|
||||||
|
|
||||||
|
if(self::$defaultExceptionHandler !== null) {
|
||||||
|
$class = new self::$defaultExceptionHandler();
|
||||||
|
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function addExceptionHandler($handler) {
|
public static function setDefaultExceptionHandler($handler) {
|
||||||
self::$exceptionHandlers[] = $handler;
|
self::$defaultExceptionHandler = $handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,12 +39,16 @@ class RouterBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backstack = false) {
|
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backstack = false, $group = null) {
|
||||||
// Loop through each route-request
|
// Loop through each route-request
|
||||||
|
|
||||||
|
$activeGroup = null;
|
||||||
|
|
||||||
/* @var $route RouterEntry */
|
/* @var $route RouterEntry */
|
||||||
foreach($routes as $route) {
|
foreach($routes as $route) {
|
||||||
|
|
||||||
|
$route->setGroup($group);
|
||||||
|
|
||||||
if($this->defaultNamespace && !$route->getNamespace()) {
|
if($this->defaultNamespace && !$route->getNamespace()) {
|
||||||
$namespace = null;
|
$namespace = null;
|
||||||
if ($route->getNamespace()) {
|
if ($route->getNamespace()) {
|
||||||
@@ -75,6 +79,7 @@ class RouterBase {
|
|||||||
$this->currentRoute = $route;
|
$this->currentRoute = $route;
|
||||||
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
|
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
|
||||||
$route->renderRoute($this->request);
|
$route->renderRoute($this->request);
|
||||||
|
$activeGroup = $route;
|
||||||
}
|
}
|
||||||
$this->currentRoute = null;
|
$this->currentRoute = null;
|
||||||
|
|
||||||
@@ -83,7 +88,7 @@ class RouterBase {
|
|||||||
$this->backstack = array();
|
$this->backstack = array();
|
||||||
|
|
||||||
// Route any routes added to the backstack
|
// Route any routes added to the backstack
|
||||||
$this->processRoutes($backstack, $mergedSettings, $newPrefixes, true);
|
$this->processRoutes($backstack, $mergedSettings, $newPrefixes, true, $activeGroup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -317,6 +317,15 @@ abstract class RouterEntry {
|
|||||||
return $this->settings['requestMethods'];
|
return $this->settings['requestMethods'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getGroup() {
|
||||||
|
return $this->group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setGroup($group) {
|
||||||
|
$this->group = $group;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
abstract function matchRoute(Request $request);
|
abstract function matchRoute(Request $request);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -33,4 +33,13 @@ class RouterGroup extends RouterEntry {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setExceptionHandler($class) {
|
||||||
|
$this->exceptionHandler = $class;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getExceptionHandler() {
|
||||||
|
return $this->exceptionHandler;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
class RouterRoute extends RouterEntry {
|
class RouterRoute extends RouterEntry {
|
||||||
|
|
||||||
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?\?{0,1})}';
|
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)\?{0,1}}';
|
||||||
|
|
||||||
protected $url;
|
protected $url;
|
||||||
|
|
||||||
@@ -60,12 +60,12 @@ class RouterRoute extends RouterEntry {
|
|||||||
|
|
||||||
$isParameter = true;
|
$isParameter = true;
|
||||||
} elseif($isParameter && $character === '}') {
|
} elseif($isParameter && $character === '}') {
|
||||||
$required = false;
|
$required = true;
|
||||||
// Check for optional parameter
|
// Check for optional parameter
|
||||||
if($lastCharacter === '?') {
|
if($lastCharacter === '?') {
|
||||||
$parameter = substr($parameter, 0, strlen($parameter)-1);
|
$parameter = substr($parameter, 0, strlen($parameter)-1);
|
||||||
$regex .= '(?:(?:\/{0,1}(?P<'.$parameter.'>[a-z0-9]*?)){0,1}\\/)';
|
$regex .= '(?:(?:\/{0,1}(?P<'.$parameter.'>[a-z0-9]*?)){0,1}\\/)';
|
||||||
$required = true;
|
$required = false;
|
||||||
} else {
|
} else {
|
||||||
// Use custom parameter regex if it exists
|
// Use custom parameter regex if it exists
|
||||||
$parameterRegex = '[a-z0-9]*?';
|
$parameterRegex = '[a-z0-9]*?';
|
||||||
@@ -130,12 +130,13 @@ class RouterRoute extends RouterEntry {
|
|||||||
public function setUrl($url) {
|
public function setUrl($url) {
|
||||||
|
|
||||||
$parameters = array();
|
$parameters = array();
|
||||||
|
$matches = array();
|
||||||
|
|
||||||
preg_match_all('/'.self::PARAMETERS_REGEX_MATCH.'/is', $url, $parameters);
|
if(preg_match_all('/'.self::PARAMETERS_REGEX_MATCH.'/is', $url, $matches)) {
|
||||||
|
$parameters = $matches[1];
|
||||||
|
}
|
||||||
|
|
||||||
$parameters = $parameters[1];
|
if(count($parameters)) {
|
||||||
|
|
||||||
if($parameters !== null) {
|
|
||||||
foreach($parameters as $param) {
|
foreach($parameters as $param) {
|
||||||
$this->parameters[$param] = '';
|
$this->parameters[$param] = '';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user