public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error) { // Return json errors if we encounter an error on the API. if (stripos($request->getUri(), '/api') !== false) { response()->json(['error' => $error->getMessage()]); } if ($error instanceof NotFoundHttpException) { $request->setUri(url('page.notfound')); return $request; } throw $error; }
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error) { echo 'ExceptionHandler 1 loaded' . chr(10); $request->setUri('/'); return $request; }
public function routeRequest($rewrite = false) { $this->loadedRoute = null; $routeNotAllowed = false; try { /* Initialize boot-managers */ if (count($this->bootManagers) > 0) { $max = count($this->bootManagers) - 1; /* @var $manager IRouterBootManager */ for ($i = $max; $i >= 0; $i--) { $manager = $this->bootManagers[$i]; $this->request = $manager->boot($this->request); if (!$this->request instanceof Request) { throw new HttpException('Bootmanager "' . get_class($manager) . '" must return instance of ' . Request::class, 500); } } } if ($rewrite === false) { /* Loop through each route-request */ $this->processRoutes($this->routes); if ($this->csrfVerifier !== null) { /* Verify csrf token for request */ $this->csrfVerifier->handle($this->request); } $this->originalUrl = $this->request->getUri(); } $max = count($this->processedRoutes) - 1; /* @var $route IRoute */ for ($i = $max; $i >= 0; $i--) { $route = $this->processedRoutes[$i]; /* If the route matches */ if ($route->matchRoute($this->request) === true) { /* Check if request method matches */ if (count($route->getRequestMethods()) > 0 && in_array($this->request->getMethod(), $route->getRequestMethods(), false) === false) { $routeNotAllowed = true; continue; } $this->loadedRoute = $route; $this->loadedRoute->loadMiddleware($this->request, $this->loadedRoute); /* If the request has changed, we reinitialize the router */ if ($this->request->getUri() !== $this->originalUrl && in_array($this->request->getUri(), $this->routeRewrites) === false) { $this->routeRewrites[] = $this->request->getUri(); $this->routeRequest(true); return; } /* Render route */ $routeNotAllowed = false; $this->request->setUri($this->originalUrl); $this->loadedRoute->renderRoute($this->request); break; } } } catch (\Exception $e) { $this->handleException($e); } if ($routeNotAllowed === true) { $this->handleException(new HttpException('Route or method not allowed', 403)); } if ($this->loadedRoute === null) { $this->handleException(new NotFoundHttpException('Route not found: ' . $this->request->getUri(), 404)); } }