/** * 处理指定的请求 * * @param Request $request 请求实例 * @return Response 产生的响应 * @throws NotFoundHttpException 如果请求的路由无效 */ public function handleRequest($request) { if (empty($this->catchAll)) { list($route, $params) = $request->resolve(); } else { $route = $this->catchAll[0]; $params = $this->catchAll; unset($params[0]); } try { Leaps::trace("Route requested: '{$route}'", __METHOD__); $this->requestedRoute = $route; $result = $this->runAction($route, $params); if ($result instanceof Response) { return $result; } else { $response = $this->getResponse(); if ($result !== null) { $response->data = $result; } return $response; } } catch (InvalidRouteException $e) { throw new NotFoundHttpException(Leaps::t('leaps', 'Page not found.'), $e->getCode(), $e); } }
/** * Parses the user request. * * @param Request $request the request component * @return array|boolean the route and the associated parameters. The latter is always empty * if [[enablePrettyUrl]] is false. False is returned if the current request cannot be successfully parsed. */ public function parseRequest($request) { if ($this->enablePrettyUrl) { $pathInfo = $request->getPathInfo(); /* @var $rule UrlRule */ foreach ($this->rules as $rule) { if (($result = $rule->parseRequest($this, $request)) !== false) { return $result; } } if ($this->enableStrictParsing) { return false; } Leaps::trace('No matching URL rules. Using default URL parsing logic.', __METHOD__); $suffix = (string) $this->suffix; if ($suffix !== '' && $pathInfo !== '') { $n = strlen($this->suffix); if (substr_compare($pathInfo, $this->suffix, -$n, $n) === 0) { $pathInfo = substr($pathInfo, 0, -$n); if ($pathInfo === '') { // suffix alone is not allowed return false; } } else { // suffix doesn't match return false; } } return [$pathInfo, []]; } else { Leaps::trace('Pretty URL not enabled. Using default URL parsing logic.', __METHOD__); $route = $request->getQueryParam($this->routeParam, ''); if (is_array($route)) { $route = ''; } return [(string) $route, []]; } }
/** * Negotiates the application language. * * @param Request $request * @return string the chosen language */ protected function negotiateLanguage($request) { if (!empty($this->languageParam) && ($language = $request->get($this->languageParam)) !== null) { if (isset($this->languages[$language])) { return $this->languages[$language]; } foreach ($this->languages as $key => $supported) { if (is_int($key) && $this->isLanguageSupported($language, $supported)) { return $supported; } } return reset($this->languages); } foreach ($request->getAcceptableLanguages() as $language) { if (isset($this->languages[$language])) { return $this->languages[$language]; } foreach ($this->languages as $key => $supported) { if (is_int($key) && $this->isLanguageSupported($language, $supported)) { return $supported; } } } return reset($this->languages); }
/** * Checks whether the Web user is allowed to perform the specified action. * @param Action $action the action to be performed * @param User $user the user object * @param Request $request * @return boolean|null true if the user is allowed, false if the user is denied, null if the rule does not apply to the user */ public function allows($action, $user, $request) { if ($this->matchAction($action) && $this->matchRole($user) && $this->matchIP($request->getUserIP()) && $this->matchVerb($request->getMethod()) && $this->matchController($action->controller) && $this->matchCustom($action)) { return $this->allow ? true : false; } else { return null; } }
/** * Parses the given request and returns the corresponding route and parameters. * @param UrlManager $manager the URL manager * @param Request $request the request component * @return array|boolean the parsing result. The route and the parameters are returned as an array. * If false, it means this rule cannot be used to parse this path info. */ public function parseRequest($manager, $request) { if ($this->mode === self::CREATION_ONLY) { return false; } if (!empty($this->verb) && !in_array($request->getMethod(), $this->verb, true)) { return false; } $pathInfo = $request->getPathInfo(); $suffix = (string) ($this->suffix === null ? $manager->suffix : $this->suffix); if ($suffix !== '' && $pathInfo !== '') { $n = strlen($suffix); if (substr_compare($pathInfo, $suffix, -$n, $n) === 0) { $pathInfo = substr($pathInfo, 0, -$n); if ($pathInfo === '') { // suffix alone is not allowed return false; } } else { return false; } } if ($this->host !== null) { $pathInfo = strtolower($request->getHostInfo()) . ($pathInfo === '' ? '' : '/' . $pathInfo); } if (!preg_match($this->pattern, $pathInfo, $matches)) { return false; } foreach ($this->defaults as $name => $value) { if (!isset($matches[$name]) || $matches[$name] === '') { $matches[$name] = $value; } } $params = $this->defaults; $tr = []; foreach ($matches as $name => $value) { if (isset($this->_routeParams[$name])) { $tr[$this->_routeParams[$name]] = $value; unset($params[$name]); } elseif (isset($this->_paramRules[$name])) { $params[$name] = $value; } } if ($this->_routeRule !== null) { $route = strtr($this->route, $tr); } else { $route = $this->route; } Leaps::trace("Request parsed with URL rule: {$this->name}", __METHOD__); return [$route, $params]; }