public function dispatch($method = 'GET', $uri = '', $domain = null) { if ($domain !== null) { $uri = rtrim($domain) . $this->slashUri($uri); try { $route = $this->dispatch($method, $uri); if ($route instanceof Route) { return $route; } } catch (NotFoundException $e) { //supress and try without domain } } $uri = $this->slashUri($uri); //lookup exact matches first if ($route = $this->findExactRoute($method, $uri)) { return $route; } //find possible matches via segment count $uriSegments = array_filter(explode('/', $uri)); $possibleRoutes = isset($this->segmentsMap[count($uriSegments)]) ? $this->segmentsMap[count($uriSegments)] : []; $route = null; foreach ($possibleRoutes as $routeGroup) { //loop each route for this uri and assign if method matches $routes = $this->routes[$routeGroup]; //test the first route in the array, the uris are the same so it either matches or it doesn't $first = new Route($routes[0]); //add global matchers/modifyers $first->addModifyers($this->modifyers); $first->addMatchers($this->matchers); if (!$first->matches($uri)) { continue; } //store params so we dont have to parse them more than once $params = $first->getParams(); //we need to store methods to send back later $methods = []; //loop the group to find a route with the method foreach ($routes as $_route) { //save methods as we found a match, but maybe not the right method $methods = array_merge($methods, $_route['methods']); //not this route, goto the next one if (!in_array($method, $_route['methods'])) { continue; } //great, the uri matches and the method is allowed, lets new up a route object $_route = new Route($_route); //add global matchers/modifyers $_route->addModifyers($this->modifyers); $_route->addMatchers($this->matchers); //fill params $_route->setParams($params); //set the route variable $route = $_route; //we found it! break; } //the route uri exists but there isn't a route matching this method if (!$route instanceof Route) { throw new MethodNotAllowedException($methods, sprintf('The uri requested: %s isn\'t allowed via: %s', $uri, $method)); } //all good return return $route; } //if we get here we haven't found a route :-( throw new NotFoundException(sprintf('The uri requested: %s cannot be found', $uri)); }