Merge branch 'master' of https://github.com/eatonphil/wellrested into eatonphil-master
This commit is contained in:
commit
1f6e1f3e9c
|
|
@ -53,10 +53,10 @@ WellRESTed's primary goal is to facilitate mapping of URIs to classes that will
|
|||
// Build the router.
|
||||
$myRouter = new Router();
|
||||
$myRouter->addRoutes(array(
|
||||
new StaticRoute("/", "\\myapi\\Handlers\\RootHandler")),
|
||||
new StaticRoute("/cats/", "\\myapi\\Handlers\\CatCollectionHandler")),
|
||||
new TemplateRoute("/cats/{id}/", "\\myapi\\Handlers\\CatItemHandler"))
|
||||
);
|
||||
new StaticRoute("/", "\\myapi\\Handlers\\RootHandler"),
|
||||
new StaticRoute("/cats/", "\\myapi\\Handlers\\CatCollectionHandler"),
|
||||
new TemplateRoute("/cats/{id}/", "\\myapi\\Handlers\\CatItemHandler")
|
||||
));
|
||||
$myRouter->respond();
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -23,11 +23,14 @@ class Router implements HandlerInterface
|
|||
{
|
||||
/** @var array Array of Route objects */
|
||||
private $routes;
|
||||
/** @var array Array of Route objects for error handling. */
|
||||
private $errorHandlers;
|
||||
|
||||
/** Create a new Router. */
|
||||
public function __construct()
|
||||
{
|
||||
$this->routes = array();
|
||||
$this->errorHandlers = array();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -73,6 +76,29 @@ class Router implements HandlerInterface
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a custom error handler.
|
||||
*
|
||||
* @param integer $error The error code.
|
||||
* @param HandlerInterface $errorHandler The handler for the error.
|
||||
*/
|
||||
public function addErrorHandler($error, $errorHandler)
|
||||
{
|
||||
$this->errorHandlers[$error] = $errorHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add custom error handlers.
|
||||
*
|
||||
* @param array $errorHandlers An array mapping an integer error code to something implementing an HandlerInterface.
|
||||
*/
|
||||
public function addErrorHandlers(array $errorHandlers)
|
||||
{
|
||||
foreach ($errorHandlers as $error => $errorHandler) {
|
||||
$this->addErrorHandler($error, $errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch the singleton Request through the router and output the response.
|
||||
*
|
||||
|
|
@ -85,6 +111,11 @@ class Router implements HandlerInterface
|
|||
if (!$response) {
|
||||
$response = $this->getNoRouteResponse($request);
|
||||
}
|
||||
$status = $response->getStatusCode();
|
||||
if (array_key_exists($status, $this->errorHandlers)) {
|
||||
$errorHandler = new $this->errorHandlers[$status]();
|
||||
$response = $errorHandler->getResponse($request, $args);
|
||||
}
|
||||
$response->respond();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue