public function dispatchRequest(ServerRequestInterface $request) : ResponseInterface
 {
     $router = null;
     try {
         /* @var $router Router */
         $router = $this->getDI()->get('router');
         try {
             $match = $router->match($request);
             return $this->getRouteHandler()->handle($match, $request);
         } catch (NotFound $e) {
             if ($router && ($notFoundHandler = $router->getNotFoundHandler())) {
                 $notFoundRoute = new Route();
                 $notFoundRoute->handler($notFoundHandler);
                 return $this->getRouteHandler()->handle($notFoundRoute, $request);
             }
             return new HtmlResponse('Page not found!', 404);
         }
     } catch (\Exception $e) {
         if ($this->application->isDev()) {
             // Whoops will display a nice error message
             throw $e;
         } else {
             if ($router && ($errorHandler = $router->getErrorHandler())) {
                 $errorRoute = new Route();
                 $errorRoute->handler($errorHandler);
                 return $this->getRouteHandler()->handle($errorRoute, $request);
             }
             return new EmptyResponse(500);
         }
     }
 }
Beispiel #2
0
 public static function multipleRoutes(array $controllers)
 {
     $routes = [];
     foreach ($controllers as $method => $controller) {
         $route = new Route();
         $route->handler($controller);
         $route->allows(strtoupper($method));
         $routes[] = $route;
     }
     return new static($routes);
 }
Beispiel #3
0
 private function addRoutes(array $routes)
 {
     $map = $this->routerContainer->getMap();
     $newRoutes = [];
     foreach ($routes as $path => $route) {
         if (!is_string($path)) {
             throw new \Exception('The routes array must be indexed by URI paths, got an integer instead');
         }
         if ($route instanceof RouteBuilder) {
             $subRoutes = $route->getRoutes();
             foreach ($subRoutes as $subRoute) {
                 $newRoutes[] = $this->prepareRoute($subRoute, $path);
             }
         } else {
             $controller = $route;
             $route = new Route();
             $route->handler($controller);
             $newRoutes[] = $this->prepareRoute($route, $path);
         }
     }
     $map->setRoutes($newRoutes);
 }