mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-20 10:11:25 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 810b80487d | |||
| 1420203149 | |||
| 18a9df56ca | |||
| f7af53a9af | |||
| 6b8351f1b8 | |||
| 6e14ded03f | |||
| eb3ddf2bf7 | |||
| 2afe784f47 | |||
| 899081f8d8 | |||
| 94e98ad5f0 | |||
| e7b9206bc9 | |||
| 8f24256434 | |||
| ec355c90b5 |
@@ -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
|
||||
|
||||
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:
|
||||
@@ -384,7 +415,7 @@ Example:
|
||||
* @return \Pecee\Http\Input\Input
|
||||
*/
|
||||
function input() {
|
||||
return new \Pecee\Http\Input\Input();
|
||||
return \Pecee\Http\Request::getInstance()->getInput();
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace Pecee\Http\Input;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class Input {
|
||||
|
||||
/**
|
||||
@@ -55,16 +57,18 @@ class Input {
|
||||
return $element;
|
||||
}
|
||||
|
||||
$element = $this->post->findFirst($index);
|
||||
if(Request::getInstance()->getMethod() !== 'get') {
|
||||
|
||||
if($element !== null) {
|
||||
return $element;
|
||||
}
|
||||
$element = $this->post->findFirst($index);
|
||||
|
||||
$element = $this->file->findFirst($index);
|
||||
if ($element !== null) {
|
||||
return $element;
|
||||
}
|
||||
|
||||
if($element !== null) {
|
||||
return $element;
|
||||
$element = $this->file->findFirst($index);
|
||||
if ($element !== null) {
|
||||
return $element;
|
||||
}
|
||||
}
|
||||
|
||||
return $default;
|
||||
@@ -85,11 +89,15 @@ class Input {
|
||||
|
||||
if($item !== null) {
|
||||
|
||||
if($item instanceof InputFile) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
if (is_array($item->getValue())) {
|
||||
return ($key !== null && isset($item->getValue()[$key])) ? $item->getValue()[$key] : $item->getValue();
|
||||
}
|
||||
|
||||
return ($item->getValue() === null) ? $default : $item->getValue();
|
||||
return (trim($item->getValue()) === '') ? $default : $item->getValue();
|
||||
}
|
||||
|
||||
return $default;
|
||||
|
||||
@@ -125,13 +125,11 @@ class Request {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request input or default value
|
||||
* @param string $name
|
||||
* @param string $defaultValue
|
||||
* @return mixed
|
||||
* Get input class
|
||||
* @return Input
|
||||
*/
|
||||
public function getInput($name, $defaultValue) {
|
||||
return (isset($_REQUEST[$name]) ? $_REQUEST[$name] : $defaultValue);
|
||||
public function getInput() {
|
||||
return $this->input;
|
||||
}
|
||||
|
||||
public function isFormatAccepted($format) {
|
||||
|
||||
@@ -20,6 +20,7 @@ class RouterBase {
|
||||
protected $bootManagers;
|
||||
protected $baseCsrfVerifier;
|
||||
protected $middlewaresToLoad;
|
||||
protected $exceptionHandlers;
|
||||
|
||||
// TODO: clean up - cut some of the methods down to smaller pieces
|
||||
|
||||
@@ -30,6 +31,7 @@ class RouterBase {
|
||||
$this->controllerUrlMap = array();
|
||||
$this->bootManagers = array();
|
||||
$this->middlewaresToLoad = array();
|
||||
$this->exceptionHandlers = array();
|
||||
}
|
||||
|
||||
public function addRoute(RouterEntry $route) {
|
||||
@@ -91,6 +93,9 @@ class RouterBase {
|
||||
|
||||
// Load middleware on group if route matches
|
||||
if($route->getPrefix() !== null && $route->matchRoute($this->request)) {
|
||||
if($route->getExceptionHandler() !== null) {
|
||||
$this->exceptionHandlers[] = $route->getExceptionHandler();
|
||||
}
|
||||
$this->middlewaresToLoad[] = $route;
|
||||
}
|
||||
}
|
||||
@@ -111,13 +116,6 @@ class RouterBase {
|
||||
|
||||
$originalUri = $this->request->getUri();
|
||||
|
||||
// Load group middlewares
|
||||
|
||||
/* @var $middleware RouterEntry */
|
||||
foreach($this->middlewaresToLoad as $middleware) {
|
||||
$middleware->loadMiddleware($this->request);
|
||||
}
|
||||
|
||||
// Initialize boot-managers
|
||||
if(count($this->bootManagers)) {
|
||||
/* @var $manager RouterBootManager */
|
||||
@@ -138,6 +136,12 @@ class RouterBase {
|
||||
// Loop through each route-request
|
||||
$this->processRoutes($this->routes);
|
||||
|
||||
// Load group middlewares
|
||||
/* @var $route RouterEntry */
|
||||
foreach($this->middlewaresToLoad as $route) {
|
||||
$route->loadMiddleware($this->request);
|
||||
}
|
||||
|
||||
$routeNotAllowed = false;
|
||||
|
||||
// Make sure routes with longer urls are rendered first
|
||||
@@ -187,14 +191,15 @@ class RouterBase {
|
||||
}
|
||||
|
||||
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) {
|
||||
if($this->request->loadedRoute !== null && $this->request->loadedRoute->exceptionHandler !== null) {
|
||||
$handler = new $this->request->loadedRoute->exceptionHandler();
|
||||
if(!($handler instanceof IExceptionHandler)) {
|
||||
|
||||
foreach ($this->exceptionHandlers as $handler) {
|
||||
$handler = new $handler($this->request);
|
||||
if (!($handler instanceof IExceptionHandler)) {
|
||||
throw new RouterException('Exception handler must implement the IExceptionHandler interface.');
|
||||
}
|
||||
|
||||
@@ -309,7 +314,7 @@ class RouterBase {
|
||||
return '';
|
||||
}
|
||||
|
||||
protected function processUrl($route, $method = null, $parameters = null, $getParams = null) {
|
||||
protected function processUrl(RouterRoute $route, $method = null, $parameters = null, $getParams = null) {
|
||||
|
||||
$domain = '';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user