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);
         }
     }
 }
Example #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);
 }
Example #3
0
 public function testCfarExceptionisThrown()
 {
     $route = $this->route->path('/users/10/adelowo')->attributes(["10", "adelowo"])->handler(UnKnownController::class)->extras(["listener" => "showUser"]);
     //This is discarded as of >=1.2 and the `indexAction` method would be invoked.
     try {
         $cfar = $this->getCfar($route);
         $cfar->dispatch();
     } catch (CfarException $e) {
         $this->assertStringStartsWith("Invalid route declaration", $e->getMessage());
         $this->assertEquals("indexAction", $cfar->getMethod());
     }
 }
 /**
  * @param Regex  $regex
  * @param string $path
  * @param null   $name
  */
 public function __construct(Regex $regex, $path, $name = null)
 {
     $path = str_replace('//', '/', $path);
     return parent::__construct($regex, $path, $name);
 }
Example #5
0
 private function prepareRoute(Route $route, string $path) : Route
 {
     if (!$route->allows) {
         $route->allows('GET');
     }
     $route->path($path);
     return $route;
 }
Example #6
0
 /**
  * 
  * Modifies the newly-added route to set 'controller' and 'action' values
  * if they are not already present.  Uses the route name to do so.
  * 
  * @param Route $route The newly-added route.
  * 
  * @return null
  * 
  */
 protected function routeCallable(Route $route)
 {
     $action = $route->name;
     if (!$action) {
         return;
     }
     $controller = null;
     $pos = strrpos($action, '.');
     if ($pos !== false) {
         $controller = substr($action, 0, $pos);
         $action = substr($action, $pos + 1);
     }
     if (!isset($route->values['controller'])) {
         $route->addValues(array('controller' => $controller));
     }
     if (!isset($route->values['action'])) {
         $route->addValues(array('action' => $action));
     }
 }
 /**
  *
  * Modifies the newly-added route to set an 'action' value from the route
  * name.
  *
  * @param Route $route The newly-added route.
  *
  * @return null
  *
  */
 protected function routeCallable(Route $route)
 {
     if ($route->name && !isset($route->values['action'])) {
         $route->addValues(array('action' => $route->name));
     }
 }