/**
  * Call
  *
  * This method finds and iterates all route objects that match the current request URI.
  */
 public function call()
 {
     try {
         if (isset($this->environment['slim.flash'])) {
             $this->view()->setData('flash', $this->environment['slim.flash']);
         }
         $this->applyHook('slim.before');
         ob_start();
         $this->applyHook('slim.before.router');
         $dispatched = false;
         $matchedRoutes = $this->router->getMatchedRoutes($this->request->getMethod(), $this->request->getResourceUri());
         foreach ($matchedRoutes as $route) {
             try {
                 $this->applyHook('slim.before.dispatch');
                 $dispatched = $this->router->dispatch($route);
                 $this->applyHook('slim.after.dispatch');
                 if ($dispatched) {
                     break;
                 }
             } catch (\Slim\Exception\Pass $e) {
                 continue;
             }
         }
         if (!$dispatched) {
             $this->notFound();
         }
         $this->applyHook('slim.after.router');
         $this->stop();
     } catch (\Slim\Exception\Stop $e) {
         $this->response()->write(ob_get_clean());
         $this->applyHook('slim.after');
     } catch (\Exception $e) {
         \Log::log_slim_exception($e);
         // Statamic's error logger
         if ($this->config('debug')) {
             throw $e;
         } else {
             try {
                 $this->error($e);
             } catch (\Slim\Exception\Stop $e) {
                 // Do nothing
             }
         }
     }
 }
 /**
  * @param  Request $request
  * @return RouteResult
  */
 public function match(Request $request)
 {
     $matchedRoutes = $this->router->getMatchedRoutes($request->getMethod(), $request->getUri()->getPath());
     if (count($matchedRoutes) === 0) {
         return RouteResult::fromRouteFailure();
     }
     /** @var \Slim\Route $matchedRoute */
     $matchedRoute = array_shift($matchedRoutes);
     $params = $matchedRoute->getParams();
     // Get the middleware from the route params and remove it
     $middleware = $params['middleware'];
     unset($params['middleware']);
     return RouteResult::fromRouteMatch($matchedRoute->getName(), $middleware, $params);
 }
Example #3
0
 /**
  * Call
  *
  * This method finds and iterates all route objects that match the current request URI.
  */
 public function call()
 {
     try {
         if (isset($this->environment['slim.flash'])) {
             $this->view()->setData('flash', $this->environment['slim.flash']);
         }
         $this->applyHook('slim.before');
         ob_start();
         $this->applyHook('slim.before.router');
         $dispatched = false;
         $httpMethodsAllowed = array();
         $this->router->setResourceUri($this->request->getResourceUri());
         $this->router->getMatchedRoutes();
         foreach ($this->router as $route) {
             if ($route->supportsHttpMethod($this->environment['REQUEST_METHOD'])) {
                 try {
                     $this->applyHook('slim.before.dispatch');
                     $dispatched = $this->router->dispatch($route);
                     $this->applyHook('slim.after.dispatch');
                     if ($dispatched) {
                         break;
                     }
                 } catch (\Slim\Exception\Pass $e) {
                     continue;
                 }
             } else {
                 $httpMethodsAllowed = array_merge($httpMethodsAllowed, $route->getHttpMethods());
             }
         }
         if (!$dispatched) {
             if ($httpMethodsAllowed) {
                 $this->response['Allow'] = implode(' ', $httpMethodsAllowed);
                 $this->halt(405, 'HTTP method not allowed for the requested resource. Use one of these instead: ' . implode(', ', $httpMethodsAllowed));
             } else {
                 $this->notFound();
             }
         }
         $this->applyHook('slim.after.router');
         $this->stop();
     } catch (\Slim\Exception\Stop $e) {
         $this->response()->write(ob_get_clean());
         $this->applyHook('slim.after');
     } catch (\Slim\Exception\RequestSlash $e) {
         $this->response->redirect($this->request->getPath() . '/', 301);
     } catch (\Exception $e) {
         if ($this->config('debug')) {
             throw $e;
         } else {
             try {
                 $this->error($e);
             } catch (\Slim\Exception\Stop $e) {
                 // Do nothing
             }
         }
     }
 }
 public function testAddRouteWithAnyMethod()
 {
     $route = new Route('/foo/bar', 'Home', Route::HTTP_METHOD_ANY, 'home');
     $this->router->addRoute($route);
     $this->assertCount(1, $this->slimRouter->getMatchedRoutes('GET', '/foo/bar'));
     $this->assertCount(1, $this->slimRouter->getMatchedRoutes('POST', '/foo/bar'));
     $this->assertCount(1, $this->slimRouter->getMatchedRoutes('PUT', '/foo/bar'));
     $this->assertCount(1, $this->slimRouter->getMatchedRoutes('DELETE', '/foo/bar'));
     $this->assertCount(1, $this->slimRouter->getMatchedRoutes('PATCH', '/foo/bar'));
     $this->assertCount(1, $this->slimRouter->getMatchedRoutes('OPTIONS', '/foo/bar'));
     $this->assertCount(1, $this->slimRouter->getMatchedRoutes('HEAD', '/foo/bar'));
 }
 public function getMatchedRoutes($httpMethod, $resourceUri, $reload = false)
 {
     // Force a reload of all matched routes
     return parent::getMatchedRoutes($httpMethod, $resourceUri, true);
 }