/** * (non-PHPdoc) * * @see \Slince\Router\MatcherInterface::match() */ function match($path, RouteCollection $routes) { $path = '/' . ltrim($path, '/'); $this->_context->setParameter('path', $path); //如果匹配子集前缀则进入子集匹配 $prefix = strtok($path, '/'); if ($routes->hasCollection($prefix)) { return $this->match(substr($path, strlen($prefix) + 1), $routes->getCollection($prefix)); } //查找符合条件的route foreach ($routes as $route) { if ($this->_validate($route)) { $this->_handleRouteParameters($route); $this->_handleRouteAction($route); return $route; } } //未找到route时,检测是否有被MethodValidator拦截的route, //有则抛MethodNotAllowedException,无抛RouteNotFoundException if (!empty($this->_report[MethodValidator::$id])) { $methods = array_map(function (RouteInterface $route) { return $route->getMethods(); }, $this->_report[MethodValidator::$id]); $methods = array_unique(call_user_func_array('array_merge', $methods)); throw new MethodNotAllowedException($methods); } throw new RouteNotFoundException(); }
/** * (non-PHPdoc) * @see \Slince\Router\Validator\ValidatorInterface::validate() */ function validate(RouteInterface $route, RequestContext $context) { $matches = []; $result = !$route->getCompiledRoute()->getHostRegex() || preg_match($route->getCompiledRoute()->getHostRegex(), $context->getHost(), $matches); if ($result) { $route->setReport(self::$id, $matches); } return $result; }
/** * (non-PHPdoc) * @see \Slince\Router\Validator\ValidatorInterface::validate() */ function validate(RouteInterface $route, RequestContext $context) { $matches = []; $result = preg_match($route->getCompiledRoute()->getRegex(), rawurldecode($context->getParameter('path')), $matches); if ($result) { $route->setReport(self::$id, $matches); } return $result; }
function testRouter() { $context = RequestContext::create(); //$context->setHost('m.baidu.com'); $router = RouterFactory::create($context); $routes = $router->getRoutes(); $routes->http('/user2', ['name' => 'home.dash', 'action' => 'UsersController@dashboard']); $routes->prefix('user', function (RouteCollection $routes) { $routes->http('/users', 'UsersController@index'); $routes->http('/users/{id}', 'UsersController@home')->setRequirements(['id' => '\\d+', 'subdomain' => '((www|m).)?', 'maindomain' => 'baidu'])->setDomain('{subdomain}{maindomain}.com'); $routes->prefix('me', function (RouteCollection $routes) { $routes->http('/account', 'UsersController@me'); }); }); try { // $route = $router->match('/user/users/2'); $route = $router->match('/user/me/account'); } catch (\Exception $e) { throw $e; } // var_dump($route->getPrefix()); // print_r($route->getCompiledRoute()->getStaticPrefix()); // print_r($route->getCompiledRoute()->getRegex()); // print_r($route->getCompiledRoute()->getHostRegex()); // print_r($route->getCompiledRoute()->getTokens()); // print_r($route->getCompiledRoute()->getHostTokens()); // print_r($route->getCompiledRoute()->getPathVariables()); // print_r($route->getCompiledRoute()->getHostVariables()); // print_r($route->getReport()); // print_r($route->getCompiledRoute()->getVariables());exit; // print_r($route->getRouteParameters());exit; // echo $router->generate($route, ['id'=>'2', 'maindomain'=> 'baidu', 'ukey'=>1, 'type'=>3]); echo $router->generateByAction('/UsersController@dashboard', [], true); echo $router->generateByName('home.dash', ['a' => 'b'], true); }
/** * 获取route的domain * @param RouteInterface $route * @return string */ protected function _getRouteDomain(RouteInterface $route) { // 如果route没有主机域名限制则直接使用环境中主机 $requireDomain = $route->getDomain(); if (empty($requireDomain)) { return $this->_context->getHost(); } // 有限制则根据route的host限制生成域名 return $this->_formateRouteParameters($requireDomain, $this->_routeParameters, $route->getRequirements()); }
/** * (non-PHPdoc) * @see \Slince\Router\Validator\ValidatorInterface::validate() */ function validate(RouteInterface $route, RequestContext $context) { return !$route->getMethods() || in_array($context->getMethod(), $route->getMethods()); }