route) { if (empty($params['alias'])) { throw new InvalidConfigException('Ticket alias can not be empty'); } $mode = isset($params['mode']) ? $params['mode'] : null; $lang = (isset($params['language']) && isset(LanguageHelper::$db2jsMap[$params['language']])) ? LanguageHelper::$db2jsMap[$params['language']] : null; $result = ($lang ? $lang . '/' : '') . self::PREFIX . $params['alias']; switch ($mode) { case self::MODE_SERVER: // prop/2342-15-Kvitnia-Street-1-Ternopil // uk/prop/2342-15-Kvitnia-Street-1-Ternopil return $result; case self::MODE_CLIENT: // #!/prop/2342-15-Kvitnia-Street-1-Ternopil // #!/uk/prop/2342-15-Kvitnia-Street-1-Ternopil return '#!/' . $result; case self::MODE_ESCAPED_FRAGMENT: // ?_escaped_fragment_=/prop/2342-15-Kvitnia-Street-1-Ternopil // ?_escaped_fragment_=/uk/prop/2342-15-Kvitnia-Street-1-Ternopil return '?_escaped_fragment_=/' . $result; default: throw new InvalidConfigException('Wrong mode "' . $mode . '"'); } } return false; } /** * URL patterns handled by rule: * - ?_escaped_fragment_=/prop/2342-15-Kvitnia-Street-1-Ternopil * - ?_escaped_fragment_=/uk/prop/2342-15-Kvitnia-Street-1-Ternopil * * @param \yii\web\UrlManager $manager * @param \yii\web\Request $request * * @return array|bool * @throws \yii\base\InvalidConfigException */ public function parseRequest($manager, $request) { $params = ['language' => null, 'id' => null, 'alias' => null, 'mode' => self::MODE_SERVER]; $query = $request->getQueryParams(); if ($query && isset($query['_escaped_fragment_']) && 1 === count($query)) { $pathInfo = $query['_escaped_fragment_']; $pathInfo = ('/' === $pathInfo[0]) ? substr($pathInfo, 1) : $pathInfo; $params['mode'] = self::MODE_ESCAPED_FRAGMENT; } else { return false; } $prefixLen = strlen(self::PREFIX); if (strlen($pathInfo) < $prefixLen) { return false; } // handle and cut lang prefix "uk/" if ('/' === $pathInfo[2] && ($lang = substr($pathInfo, 0, 2)) && isset(LanguageHelper::$js2dbMap[$lang])) { $params['language'] = LanguageHelper::$js2dbMap[$lang]; $pathInfo = substr($pathInfo, 3); } // handle 'prop/' prefix if (self::PREFIX === substr($pathInfo, 0, $prefixLen)) { if (false === ($dashIdx = strpos($pathInfo, '-', $prefixLen))) { $dashIdx = strlen($pathInfo); } $params['id'] = substr($pathInfo, $prefixLen, $dashIdx - $prefixLen); if (is_numeric($params['id'])) { $params['alias'] = substr($pathInfo, $prefixLen); return [$this->route, $params]; } } return false; } // /** // * URL patterns handled by rule: // * - prop/2342-15-Kvitnia-Street-1-Ternopil // * - uk/prop/2342-15-Kvitnia-Street-1-Ternopil // * - ?_escaped_fragment_=/prop/2342-15-Kvitnia-Street-1-Ternopil // * - ?_escaped_fragment_=/uk/prop/2342-15-Kvitnia-Street-1-Ternopil // * // * @param \yii\web\UrlManager $manager // * @param \yii\web\Request $request // * // * @return array|bool // * @throws \yii\base\InvalidConfigException // */ // public function parseRequest($manager, $request) // { // $params = ['language' => null, 'id' => null, 'alias' => null, 'mode' => self::MODE_SERVER]; // $pathInfo = $request->getPathInfo(); // $query = $request->getQueryParams(); // // if ($pathInfo && $query || empty($pathInfo) && empty($query)) { // return false; // } // // $prefixLen = strlen(self::PREFIX); // // if ('' === $pathInfo && isset($query['_escaped_fragment_']) && 1 === count($query)) { // $pathInfo = $query['_escaped_fragment_']; // $pathInfo = ('/' === $pathInfo[0]) ? substr($pathInfo, 1) : $pathInfo; // $params['mode'] = self::MODE_ESCAPED_FRAGMENT; // } // // if (strlen($pathInfo) < $prefixLen) { // return false; // } // // // handle and cut lang prefix "uk/" // if ('/' === $pathInfo[2] && ($lang = substr($pathInfo, 0, 2)) && isset(LanguageHelper::$js2dbMap[$lang])) { // $params['language'] = LanguageHelper::$js2dbMap[$lang]; // $pathInfo = substr($pathInfo, 3); // } // // // handle 'prop/' prefix // if (self::PREFIX === substr($pathInfo, 0, $prefixLen)) { // $dashIdx = strpos($pathInfo, '-', $prefixLen); // if (false !== $dashIdx) { // $params['id'] = substr($pathInfo, $prefixLen, $dashIdx - $prefixLen); // if (is_numeric($params['id'])) { // $params['alias'] = substr($pathInfo, $prefixLen); // return [$this->route, $params]; // } // } // } // // return false; // } }