repository = $repository; $this->cache = $cache; } /** * @param UrlManager $manager * @param Request $request * @return array|false * @throws UrlNormalizerRedirectException */ public function parseRequest($manager, $request): bool|array { $path = $request->pathInfo; $result = $this->cache->getOrSet(['page_route', 'path' => $path], function () use ($path) { if (!$page = $this->repository->findBySlug($this->getPathSlug($path))) { return ['id' => null, 'path' => null]; } return ['id' => $page->id, 'path' => $this->getPagePath($page)]; }, 1000); if (empty($result['id'])) { return false; } if ($path != $result['path']) { throw new UrlNormalizerRedirectException(['page/view', 'id' => $result['id']], 301); } return ['page/view', ['id' => $result['id']]]; } /** * @param UrlManager $manager * @param string $route * @param array $params * @return mixed */ public function createUrl($manager, $route, $params): mixed { if ($route == 'page/view') { if (empty($params['id'])) { throw new InvalidArgumentException('Empty id.'); } $id = $params['id']; $url = $this->cache->getOrSet(['page_route', 'id' => $id], function () use ($id) { if (!$page = $this->repository->find($id)) { return null; } return $this->getPagePath($page); }); if (!$url) { return 'error404'; throw new InvalidArgumentException('Undefined id.'); } unset($params['id']); if (!empty($params) && ($query = http_build_query($params)) !== '') { $url .= '?' . $query; } return $url; } return false; } private function getPathSlug($path): string { $chunks = explode('/', $path); return end($chunks); } private function getPagePath(Page $page): string { $chunks = ArrayHelper::getColumn($page->getParents()->andWhere(['>', 'depth', 0])->all(), 'slug'); $chunks[] = $page->slug; return implode('/', $chunks); } }