Пример #1
0
 /**
  * Test the getByMethod action - expect 0 results
  */
 public function testGetByMethodWithoutResults()
 {
     $this->router->add('/', 'GET', function () {
     });
     $routes = $this->router->getRoutesByMethod('POST');
     $this->assertEquals(0, count($routes));
 }
Пример #2
0
 /**
  * Resolve the given url and call the method that belongs to the route
  *
  * @param  array $request [contains the uri and the request method]
  *
  * @return mixed
  */
 public function resolve($request)
 {
     // get all register routes with the same request method
     $routes = $this->router->getRoutesByMethod($request['method']);
     // remove trailing and leading slash
     $requestedUri = trim(preg_replace('/\\?.*/', '', $request['uri']), '/');
     // loop trough the posible routes
     foreach ($routes as $route) {
         $matches = array();
         // if the requested route matches one of the defined routes
         if ($route->getUrl() === $requestedUri || preg_match('~^' . $route->getUrl() . '$~', $requestedUri, $matches)) {
             $arguments = $this->getArguments($matches);
             if (is_object($route->getAction()) && $route->getAction() instanceof \Closure) {
                 return call_user_func_array($route->getAction(), $arguments);
             }
             $className = $route->getNamespace() . substr($route->getAction(), 0, strpos($route->getAction(), '::'));
             $functionName = substr($route->getAction(), strpos($route->getAction(), '::') + 2);
             return call_user_func_array(array(new $className(), $functionName), $arguments);
         }
     }
     // if no route is found throw an RouteNotFoundException
     throw new RouteNotFoundException($request['method'] . ' ' . $request['uri'] . ' not found');
 }