mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Merge pull request #97 from skipperbent/development
- Added custom ExceptionHandler example to documentation.
This commit is contained in:
@@ -103,6 +103,37 @@ SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\So
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### ExceptionHandler example
|
||||||
|
|
||||||
|
This is a basic example of an ExceptionHandler implementation:
|
||||||
|
|
||||||
|
```php
|
||||||
|
namespace BB\Handlers;
|
||||||
|
|
||||||
|
use Pecee\Http\Request;
|
||||||
|
use Pecee\SimpleRouter\RouterEntry;
|
||||||
|
|
||||||
|
class CustomExceptionHandler implements IExceptionHandler {
|
||||||
|
|
||||||
|
public function handleError( Request $request, RouterEntry $router = null, \Exception $error) {
|
||||||
|
|
||||||
|
// If the error-code is 404; show another route which contains the page-not-found
|
||||||
|
if($error->getCode() === 404) {
|
||||||
|
// Load your custom 404-page view
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output error as json if on api path.
|
||||||
|
if(stripos($request->getUri(), '/api') !== false) {
|
||||||
|
response()->json(['error' => $error->getMessage()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise default exception will be thrown by the router.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
``
|
||||||
|
|
||||||
### Sub-domain routing
|
### Sub-domain routing
|
||||||
|
|
||||||
Route groups may also be used to route wildcard sub-domains. 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 route wildcard sub-domains. 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:
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\Http\Input;
|
namespace Pecee\Http\Input;
|
||||||
|
|
||||||
|
use Pecee\Http\Request;
|
||||||
|
|
||||||
class Input {
|
class Input {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -55,7 +57,7 @@ class Input {
|
|||||||
return $element;
|
return $element;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(request()->getMethod() !== 'get') {
|
if(Request::getInstance()->getMethod() !== 'get') {
|
||||||
|
|
||||||
$element = $this->post->findFirst($index);
|
$element = $this->post->findFirst($index);
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ class RouterBase {
|
|||||||
protected $bootManagers;
|
protected $bootManagers;
|
||||||
protected $baseCsrfVerifier;
|
protected $baseCsrfVerifier;
|
||||||
protected $middlewaresToLoad;
|
protected $middlewaresToLoad;
|
||||||
|
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,6 +31,7 @@ class RouterBase {
|
|||||||
$this->controllerUrlMap = array();
|
$this->controllerUrlMap = array();
|
||||||
$this->bootManagers = array();
|
$this->bootManagers = array();
|
||||||
$this->middlewaresToLoad = array();
|
$this->middlewaresToLoad = array();
|
||||||
|
$this->exceptionHandlers = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addRoute(RouterEntry $route) {
|
public function addRoute(RouterEntry $route) {
|
||||||
@@ -91,6 +93,9 @@ class RouterBase {
|
|||||||
|
|
||||||
// Load middleware on group if route matches
|
// Load middleware on group if route matches
|
||||||
if($route->getPrefix() !== null && $route->matchRoute($this->request)) {
|
if($route->getPrefix() !== null && $route->matchRoute($this->request)) {
|
||||||
|
if($route->getExceptionHandler() !== null) {
|
||||||
|
$this->exceptionHandlers[] = $route->getExceptionHandler();
|
||||||
|
}
|
||||||
$this->middlewaresToLoad[] = $route;
|
$this->middlewaresToLoad[] = $route;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,14 +191,15 @@ class RouterBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!$this->request->loadedRoute) {
|
if(!$this->request->loadedRoute) {
|
||||||
throw new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404);
|
$this->handleException(new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function handleException(\Exception $e) {
|
protected function handleException(\Exception $e) {
|
||||||
if($this->request->loadedRoute !== null && $this->request->loadedRoute->exceptionHandler !== null) {
|
|
||||||
$handler = new $this->request->loadedRoute->exceptionHandler();
|
foreach ($this->exceptionHandlers as $handler) {
|
||||||
if(!($handler instanceof IExceptionHandler)) {
|
$handler = new $handler($this->request);
|
||||||
|
if (!($handler instanceof IExceptionHandler)) {
|
||||||
throw new RouterException('Exception handler must implement the IExceptionHandler interface.');
|
throw new RouterException('Exception handler must implement the IExceptionHandler interface.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user