/** * Test adding multiple routes * * @return void **/ public function testAddMultipleRoutes() { $collection = new RouteCollection(); $routes = $this->routesProvider(); $tests = []; foreach ($routes as $route) { list($method, $path, $handler, $test_group, $test_path) = $route; $collection->{$method}($path, $handler); $tests[$test_group][] = ['path' => $test_path, 'handler' => $handler]; } foreach ($tests as $group => $test) { $this->assertEquals($test, $collection->getRoutes($group)); } }
/** * Dispatch a request against a collection of routes * * @param Slab\Core\Http\RequestInterface * @param Slab\Router\RouteCollection * @return array [Code, Data] **/ public function dispatch(RequestInterface $request, RouteCollection $collection) { $method = $request->getMethod(); $routes = $collection->getRoutes($method); if (empty($routes)) { return ['status' => static::NOT_FOUND]; } $path = trim($request->getPath(), '/'); foreach ($routes as $route) { $match = $this->match($path, $route); if ($match !== false) { return ['status' => static::FOUND, 'route' => $route, 'params' => $match]; } } return ['status' => static::NOT_FOUND]; }