mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
@@ -176,20 +176,33 @@ This is a simple example of an integration into a framework.
|
|||||||
The framework has it's own ```Router``` class which inherits from the ```SimpleRouter``` class. This allows the framework to add custom functionality.
|
The framework has it's own ```Router``` class which inherits from the ```SimpleRouter``` class. This allows the framework to add custom functionality.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
namespace MyProject;
|
<?php
|
||||||
|
<?php
|
||||||
|
namespace Pecee;
|
||||||
|
|
||||||
|
use Pecee\Exception\RouterException;
|
||||||
use Pecee\Handler\ExceptionHandler;
|
use Pecee\Handler\ExceptionHandler;
|
||||||
|
use Pecee\Http\Middleware\IMiddleware;
|
||||||
use Pecee\SimpleRouter\RouterBase;
|
use Pecee\SimpleRouter\RouterBase;
|
||||||
use Pecee\SimpleRouter\SimpleRouter;
|
use Pecee\SimpleRouter\SimpleRouter;
|
||||||
|
|
||||||
class Router extends SimpleRouter {
|
class Router extends SimpleRouter {
|
||||||
|
|
||||||
protected static $defaultExceptionHandler;
|
protected static $defaultExceptionHandler;
|
||||||
|
protected static $defaultMiddlewares = array();
|
||||||
|
|
||||||
public static function start($defaultNamespace = null) {
|
public static function start($defaultNamespace = null) {
|
||||||
|
|
||||||
|
// Debug information
|
||||||
|
Debug::getInstance()->add('Router initialised.');
|
||||||
|
|
||||||
|
// Load framework specific controllers
|
||||||
|
static::get('/js-wrap', 'ControllerJs@wrap', ['namespace' => '\Pecee\Controller'])->setAlias('pecee.js.wrap');
|
||||||
|
static::get('/css-wrap', 'ControllerCss@wrap', ['namespace' => '\Pecee\Controller'])->setAlias('pecee.css.wrap');
|
||||||
|
static::get('/captcha', 'ControllerCaptcha@show', ['namespace' => '\Pecee\Controller']);
|
||||||
|
|
||||||
// Load routes.php
|
// Load routes.php
|
||||||
$file = $_ENV['base_path'] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
|
$file = $_ENV['base_path'] . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'routes.php';
|
||||||
if(file_exists($file)) {
|
if(file_exists($file)) {
|
||||||
require_once $file;
|
require_once $file;
|
||||||
}
|
}
|
||||||
@@ -199,20 +212,57 @@ class Router extends SimpleRouter {
|
|||||||
|
|
||||||
// Handle exceptions
|
// Handle exceptions
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
if(count(static::$defaultMiddlewares)) {
|
||||||
|
/* @var $middleware \Pecee\Http\Middleware\IMiddleware */
|
||||||
|
foreach(static::$defaultMiddlewares as $middleware) {
|
||||||
|
$middleware = new $middleware();
|
||||||
|
if(!($middleware instanceof IMiddleware)) {
|
||||||
|
throw new RouterException('Middleware must be implement the IMiddleware interface.');
|
||||||
|
}
|
||||||
|
$middleware->handle(RouterBase::getInstance()->getRequest());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
parent::start($defaultNamespace);
|
parent::start($defaultNamespace);
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
|
|
||||||
if(self::$defaultExceptionHandler !== null) {
|
$route = RouterBase::getInstance()->getLoadedRoute();
|
||||||
$class = new self::$defaultExceptionHandler();
|
|
||||||
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
|
// Otherwise use the fallback default exceptions handler
|
||||||
|
if(static::$defaultExceptionHandler !== null) {
|
||||||
|
static::loadExceptionHandler(static::$defaultExceptionHandler, $route, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function setDefaultExceptionHandler($handler) {
|
protected static function loadExceptionHandler($class, $route, $e) {
|
||||||
self::$defaultExceptionHandler = $handler;
|
$class = new $class();
|
||||||
|
|
||||||
|
if(!($class instanceof ExceptionHandler)) {
|
||||||
|
throw new \ErrorException('Exception handler must be an instance of \Pecee\Handler\ExceptionHandler');
|
||||||
|
}
|
||||||
|
|
||||||
|
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function defaultExceptionHandler($handler) {
|
||||||
|
static::$defaultExceptionHandler = $handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add default middleware that will be loaded before any route
|
||||||
|
* @param string|array $middlewares
|
||||||
|
*/
|
||||||
|
public static function defaultMiddleware($middlewares) {
|
||||||
|
if(is_array($middlewares)) {
|
||||||
|
static::$defaultMiddlewares = $middlewares;
|
||||||
|
} else {
|
||||||
|
static::$defaultMiddlewares[] = $middlewares;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ class RouterBase {
|
|||||||
protected $defaultNamespace;
|
protected $defaultNamespace;
|
||||||
protected $bootManagers;
|
protected $bootManagers;
|
||||||
protected $baseCsrfVerifier;
|
protected $baseCsrfVerifier;
|
||||||
protected $middlewaresToLoad;
|
|
||||||
protected $exceptionHandlers;
|
protected $exceptionHandlers;
|
||||||
|
|
||||||
// TODO: clean up - cut some of the methods down to smaller pieces
|
// TODO: clean up - cut some of the methods down to smaller pieces
|
||||||
@@ -30,7 +29,6 @@ class RouterBase {
|
|||||||
$this->backStack = array();
|
$this->backStack = array();
|
||||||
$this->controllerUrlMap = array();
|
$this->controllerUrlMap = array();
|
||||||
$this->bootManagers = array();
|
$this->bootManagers = array();
|
||||||
$this->middlewaresToLoad = array();
|
|
||||||
$this->exceptionHandlers = array();
|
$this->exceptionHandlers = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,12 +89,9 @@ class RouterBase {
|
|||||||
$route->renderRoute($this->request);
|
$route->renderRoute($this->request);
|
||||||
$mergedSettings = array_merge($settings, $route->getMergeableSettings());
|
$mergedSettings = array_merge($settings, $route->getMergeableSettings());
|
||||||
|
|
||||||
// Load middleware on group if route matches
|
// Add exceptionhandler
|
||||||
if($route->getPrefix() !== null && $route->matchRoute($this->request)) {
|
if($route->matchRoute($this->request) && $route->getExceptionHandler() !== null) {
|
||||||
if($route->getExceptionHandler() !== null) {
|
$this->exceptionHandlers[] = $route->getExceptionHandler();
|
||||||
$this->exceptionHandlers[] = $route->getExceptionHandler();
|
|
||||||
}
|
|
||||||
$this->middlewaresToLoad[] = $route;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,22 +131,8 @@ class RouterBase {
|
|||||||
// Loop through each route-request
|
// Loop through each route-request
|
||||||
$this->processRoutes($this->routes);
|
$this->processRoutes($this->routes);
|
||||||
|
|
||||||
// Load group middlewares
|
|
||||||
/* @var $route RouterEntry */
|
|
||||||
foreach($this->middlewaresToLoad as $route) {
|
|
||||||
$route->loadMiddleware($this->request);
|
|
||||||
}
|
|
||||||
|
|
||||||
$routeNotAllowed = false;
|
$routeNotAllowed = false;
|
||||||
|
|
||||||
// Make sure routes with longer urls are rendered first
|
|
||||||
usort($this->controllerUrlMap, function($a, $b) {
|
|
||||||
if(strlen($a->getUrl()) < strlen($b->getUrl())) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
});
|
|
||||||
|
|
||||||
$max = count($this->controllerUrlMap);
|
$max = count($this->controllerUrlMap);
|
||||||
|
|
||||||
/* @var $route RouterEntry */
|
/* @var $route RouterEntry */
|
||||||
|
|||||||
@@ -92,20 +92,11 @@ class RouterGroup extends RouterEntry {
|
|||||||
if($this->getNamespace() !== null && isset($settings['namespace'])) {
|
if($this->getNamespace() !== null && isset($settings['namespace'])) {
|
||||||
unset($settings['namespace']);
|
unset($settings['namespace']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(is_array($settings)) {
|
if(is_array($settings)) {
|
||||||
$this->settings = array_merge($this->settings, $settings);
|
$this->settings = array_merge($this->settings, $settings);
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMergeableSettings() {
|
|
||||||
$settings = parent::getMergeableSettings();
|
|
||||||
|
|
||||||
if(isset($settings['middleware'])) {
|
|
||||||
unset($settings['middleware']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user