yii\web\UrlManager::parseRequest PHP Method

parseRequest() public method

Parses the user request.
public parseRequest ( Request $request ) : array | boolean
$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) {
            /* @var $rule UrlRule */
            foreach ($this->rules as $rule) {
                if (($result = $rule->parseRequest($this, $request)) !== false) {
                    return $result;
                }
            }
            if ($this->enableStrictParsing) {
                return false;
            }
            Yii::trace('No matching URL rules. Using default URL parsing logic.', __METHOD__);
            $suffix = (string) $this->suffix;
            $pathInfo = $request->getPathInfo();
            $normalized = false;
            if ($this->normalizer !== false) {
                $pathInfo = $this->normalizer->normalizePathInfo($pathInfo, $suffix, $normalized);
            }
            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;
                }
            }
            if ($normalized) {
                // pathInfo was changed by normalizer - we need also normalize route
                return $this->normalizer->normalizeRoute([$pathInfo, []]);
            } else {
                return [$pathInfo, []];
            }
        } else {
            Yii::trace('Pretty URL not enabled. Using default URL parsing logic.', __METHOD__);
            $route = $request->getQueryParam($this->routeParam, '');
            if (is_array($route)) {
                $route = '';
            }
            return [(string) $route, []];
        }
    }

Usage Example

 /**
  * Parses the URL and sets the language accordingly
  * @param \yii\web\Request $request
  * @return array|bool
  */
 public function parseRequest($request)
 {
     if ($this->enablePrettyUrl) {
         $pathInfo = $request->getPathInfo();
         $language = explode('/', $pathInfo);
         $locale = $this->getLocal($language[0], $request);
         $language = $this->languages[$locale];
         if (in_array($language, $this->languages, true)) {
             $request->setPathInfo(substr_replace($pathInfo, '', 0, strlen($locale) + 1));
             Yii::$app->language = $locale;
         }
     } else {
         $params = $request->getQueryParams();
         $route = isset($params[$this->routeParam]) ? $params[$this->routeParam] : '';
         if (is_array($route)) {
             $route = '';
         }
         $language = explode('/', $route)[0];
         $locale = $this->getLocal($language, $request);
         $language = $this->languages[$locale];
         if (in_array($language, $this->languages, true)) {
             $route = substr_replace($route, '', 0, strlen($locale) + 1);
             $params[$this->routeParam] = $route;
             $request->setQueryParams($params);
             Yii::$app->language = $locale;
         }
     }
     return parent::parseRequest($request);
 }
All Usage Examples Of yii\web\UrlManager::parseRequest