/** * Handles the specified request. * @param Request $request the request to be handled * @return Response the resulting response * @throws NotFoundHttpException if the requested route is invalid */ public function handleRequest($request) { if (empty($this->catchAll)) { list($route, $params) = $request->resolve(); } else { $route = $this->catchAll[0]; $params = $this->catchAll; unset($params[0]); } if (!empty($params['param']) && empty($params['debug'])) { $decode = Urlcrypt::decode($params['param']); $s = unserialize($decode); $params = $s; } try { Yii::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(Yii::t('yii', 'Page not found.'), $e->getCode(), $e); } }
/** * Creates a URL using the given route and query parameters. * * You may specify the route as a string, e.g., `site/index`. You may also use an array * if you want to specify additional query parameters for the URL being created. The * array format must be: * * ```php * // generates: /index.php?r=site/index¶m1=value1¶m2=value2 * ['site/index', 'param1' => 'value1', 'param2' => 'value2'] * ``` * * If you want to create a URL with an anchor, you can use the array format with a `#` parameter. * For example, * * ```php * // generates: /index.php?r=site/index¶m1=value1#name * ['site/index', 'param1' => 'value1', '#' => 'name'] * ``` * * The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL. * * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route * as an absolute route. * * @param string|array $params use a string to represent a route (e.g. `site/index`), * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). * @return string the created URL */ public function createUrl($params, $encrypt = false) { $params = (array) $params; $anchor = isset($params['#']) ? '#' . $params['#'] : ''; unset($params['#'], $params[$this->routeParam]); $route = trim($params[0], '/'); unset($params[0]); $baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl(); //加密处理url if ($encrypt == true) { $a = serialize($params); $encoded = Urlcrypt::encode($a); $params = array(); $params['param'] = $encoded; } if ($this->enablePrettyUrl) { $cacheKey = $route . '?' . implode('&', array_keys($params)); /* @var $rule UrlRule */ $url = false; if (isset($this->_ruleCache[$cacheKey])) { foreach ($this->_ruleCache[$cacheKey] as $rule) { if (($url = $rule->createUrl($this, $route, $params)) !== false) { break; } } } else { $this->_ruleCache[$cacheKey] = []; } if ($url === false) { $cacheable = true; foreach ($this->rules as $rule) { if (!empty($rule->defaults) && $rule->mode !== UrlRule::PARSING_ONLY) { // if there is a rule with default values involved, the matching result may not be cached $cacheable = false; } if (($url = $rule->createUrl($this, $route, $params)) !== false) { if ($cacheable) { $this->_ruleCache[$cacheKey][] = $rule; } break; } } } if ($url !== false) { if (strpos($url, '://') !== false) { if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) { return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor; } else { return $url . $baseUrl . $anchor; } } else { return "{$baseUrl}/{$url}{$anchor}"; } } if ($this->suffix !== null) { $route .= $this->suffix; } if (!empty($params) && ($query = http_build_query($params)) !== '') { $route .= '?' . $query; } return "{$baseUrl}/{$route}{$anchor}"; } else { $url = "{$baseUrl}?{$this->routeParam}=" . urlencode($route); if (!empty($params) && ($query = http_build_query($params)) !== '') { $url .= '&' . $query; } return $url . $anchor; } }
/** * Returns the request parameters given in the [[queryString]]. * * This method will return the contents of `$_GET` if params where not explicitly set. * @return array the request GET parameter values. * @see setQueryParams() */ public function getQueryParams() { if ($this->_queryParams === null) { if (empty($_GET['param']) || !empty($_GET['debug']) && $_GET['debug'] == 1) { return $_GET; } else { $decoded = Urlcrypt::decode($_GET['param']); $s = unserialize($decoded); return $s; } } return $this->_queryParams; }