mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Major overhaul
This commit is contained in:
@@ -1,34 +1,44 @@
|
||||
<?php
|
||||
namespace Demo\Handlers;
|
||||
|
||||
use Pecee\Handler\IExceptionHandler;
|
||||
use Pecee\Handlers\IExceptionHandler;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
|
||||
class CustomExceptionHandler implements IExceptionHandler
|
||||
{
|
||||
public function handleError(Request $request, RouterEntry &$route = null, \Exception $error)
|
||||
{
|
||||
// Return json errors if we encounter an error on /api.
|
||||
|
||||
/* You can use the exception handler to format errors depending on the request and type. */
|
||||
|
||||
if (stripos($request->getUri(), '/api') !== false) {
|
||||
header('content-type: application/json');
|
||||
echo json_encode([
|
||||
|
||||
response()->json([
|
||||
'error' => $error->getMessage(),
|
||||
'code' => $error->getCode()
|
||||
'code' => $error->getCode(),
|
||||
]);
|
||||
die();
|
||||
|
||||
}
|
||||
|
||||
// else we just throw the error
|
||||
if ($error->getCode() == 404) {
|
||||
/* The router will throw the NotFoundHttpException on 404 */
|
||||
if($error instanceof NotFoundHttpException) {
|
||||
|
||||
// Return 404 path
|
||||
$request->setUri('/404');
|
||||
/*
|
||||
* Render your own custom 404-view, rewrite the request to another route,
|
||||
* or simply return the $request object to ignore the error and continue on rendering the route.
|
||||
*
|
||||
* The code below will make the router render our page.notfound route.
|
||||
*/
|
||||
|
||||
$request->setUri(url('page.notfound'));
|
||||
return $request;
|
||||
|
||||
}
|
||||
|
||||
throw $error;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user