/** * Match application action by request * * @param RequestInterface $request * * @return ActionInterface */ public function match(RequestInterface $request) { $slug = trim($request->getPathInfo(), '/blog/'); $slug = rtrim($slug, '/'); /** @var \Pyvil\Blog\Model\Post $post */ $post = $this->_postFactory->create(); $postId = $post->checkSlug($slug); if (!$postId) { return null; } $request->setModuleName('blog')->setControllerName('view')->setActionName('index')->setParam('id', $postId); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $slug); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', ['request' => $request]); }
/** * Validate and Match Blog Post and modify request * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { $url_key = trim($request->getPathInfo(), '/blog/'); $url_key = rtrim($url_key, '/'); /** @var \Ashsmith\Blog\Model\Post $post */ $post = $this->_postFactory->create(); $post_id = $post->checkUrlKey($url_key); if (!$post_id) { return null; } $request->setModuleName('blog')->setControllerName('view')->setActionName('index')->setParam('post_id', $post_id); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $url_key); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); }
/** * Validate and Match Cms Page and modify request * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { $url_key = trim($request->getPathInfo(), '/news/'); $url_key = rtrim($url_key, '/'); /** @var \Pre\News\Model\Feed $feed */ $feed = $this->_pageFactory->create(); $feed_id = $feed->checkUrlKey($url_key); if (!$feed_id) { return null; } $request->setModuleName('news')->setControllerName('view')->setActionName('index')->setParam('feed_id', $feed_id); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $url_key); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); }
/** * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { $url_key = trim($request->getPathInfo(), '/testimonial/'); $url_key = rtrim($url_key, '/'); /** @var \V3N0m21\Testimonials\Model\Testimonial $testimonial */ $testimonial = $this->_testimonialFactory->create(); $testimonial_id = $testimonial->getById($url_key); if (!$testimonial_id) { return null; } $request->setModuleName('testimonials')->setControllerName('view')->setActionName('index')->setParam('testimonial_id', $testimonial_id); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $url_key); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); }
/** * Validate and Match Blog Post and modify request * * @param RequestInterface $request * @return bool */ public function match(RequestInterface $request) { $urlKey = str_replace("/blog/", "", $request->getPathInfo()); $urlKey = rtrim($urlKey, '/'); $urlKey = ltrim($urlKey, '/'); /** @var \ISM\Blog\Model\Post $post */ $post = $this->_postFactory->create(); $id = $post->checkUrlKey($urlKey); if (!$id) { return null; } $request->setModuleName('blog')->setControllerName('view')->setActionName('index')->setParam('id', $id); $request->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $urlKey); return $this->_actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); }
/** * Validate and Match testimonials and modify request * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { // throw new \Magento\Framework\Exception\LocalizedException( // __('$request->getPathInfo() ' . $request->getPathInfo()) // ); // $url_key = trim($request->getPathInfo(), '/testimonials/'); $url_key = str_replace('/testimonials/', '', $request->getPathInfo()); $url_key = rtrim($url_key, '/'); // throw new \Magento\Framework\Exception\LocalizedException( // __('$url_key ' . $url_key) // ); /** @var \Test\Testimonials\Model\Post $post */ $post = $this->_postFactory->create(); $post_id = $post->checkUrlKey($url_key); if (!$post_id) { return null; } $request->setModuleName('testimonials')->setControllerName('view')->setActionName('index')->setParam('post_id', $post_id); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $url_key); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); }
/** * Match controller name * * @param \Magento\Framework\App\RequestInterface $request * @param string $param * @return string */ protected function matchActionPath(\Magento\Framework\App\RequestInterface $request, $param) { if ($request->getControllerName()) { $actionPath = $request->getControllerName(); } elseif (!empty($param)) { $actionPath = $param; } else { $actionPath = $this->_defaultPath->getPart('controller'); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, ltrim($request->getOriginalPathInfo(), '/')); } return $actionPath; }
/** * Validate and Match Cms Page and modify request * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { $identifier = trim($request->getPathInfo(), '/'); $condition = new \Magento\Framework\Object(array('identifier' => $identifier, 'continue' => true)); $this->_eventManager->dispatch('cms_controller_router_match_before', array('router' => $this, 'condition' => $condition)); $identifier = $condition->getIdentifier(); if ($condition->getRedirectUrl()) { $this->_response->setRedirect($condition->getRedirectUrl()); $request->setDispatched(true); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Redirect', array('request' => $request)); } if (!$condition->getContinue()) { return null; } /** @var \Magento\Cms\Model\Page $page */ $page = $this->_pageFactory->create(); $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId()); if (!$pageId) { return null; } $request->setModuleName('cms')->setControllerName('page')->setActionName('view')->setParam('page_id', $pageId); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', array('request' => $request)); }
/** * Validate and Match News Author and modify request * @param \Magento\Framework\App\RequestInterface $request * @return bool * //TODO: maybe remove this and use the url rewrite table. */ public function match(RequestInterface $request) { if (!$this->dispatched) { $urlKey = trim($request->getPathInfo(), '/'); $origUrlKey = $urlKey; /** @var Object $condition */ $condition = new Object(['url_key' => $urlKey, 'continue' => true]); $this->eventManager->dispatch('sample_news_controller_router_match_before', ['router' => $this, 'condition' => $condition]); $urlKey = $condition->getUrlKey(); if ($condition->getRedirectUrl()) { $this->response->setRedirect($condition->getRedirectUrl()); $request->setDispatched(true); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Redirect', ['request' => $request]); } if (!$condition->getContinue()) { return null; } $entities = ['author' => ['prefix' => $this->scopeConfig->getValue('sample_news/author/url_prefix', ScopeInterface::SCOPE_STORES), 'suffix' => $this->scopeConfig->getValue('sample_news/author/url_suffix', ScopeInterface::SCOPE_STORES), 'list_key' => $this->scopeConfig->getValue('sample_news/author/list_url', ScopeInterface::SCOPE_STORES), 'list_action' => 'index', 'factory' => $this->authorFactory, 'controller' => 'author', 'action' => 'view', 'param' => 'id']]; foreach ($entities as $entity => $settings) { if ($settings['list_key']) { if ($urlKey == $settings['list_key']) { $request->setModuleName('sample_news')->setControllerName($settings['controller'])->setActionName($settings['list_action']); $request->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $urlKey); $this->dispatched = true; return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', ['request' => $request]); } } if ($settings['prefix']) { $parts = explode('/', $urlKey); if ($parts[0] != $settings['prefix'] || count($parts) != 2) { continue; } $urlKey = $parts[1]; } if ($settings['suffix']) { $suffix = substr($urlKey, -strlen($settings['suffix']) - 1); if ($suffix != '.' . $settings['suffix']) { continue; } $urlKey = substr($urlKey, 0, -strlen($settings['suffix']) - 1); } /** @var \Sample\News\Model\Author $instance */ $instance = $settings['factory']->create(); $id = $instance->checkUrlKey($urlKey, $this->storeManager->getStore()->getId()); if (!$id) { return null; } $request->setModuleName('sample_news')->setControllerName('author')->setActionName('view')->setParam('id', $id); $request->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $origUrlKey); $request->setDispatched(true); $this->dispatched = true; return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', ['request' => $request]); } } return null; }
/** * Modify request path to imitate basic request * * @param \Magento\Framework\App\RequestInterface $request * @return $this */ protected function _prepareVdeRequest(\Magento\Framework\App\RequestInterface $request) { list($vdeFrontName, $designMode, $themeId) = explode('/', trim($request->getPathInfo(), '/')); $request->setAlias('editorMode', $designMode); $request->setAlias('themeId', (int) $themeId); $vdePath = implode('/', array($vdeFrontName, $designMode, $themeId)); $noVdePath = substr($request->getPathInfo(), strlen($vdePath) + 1) ?: '/'; $request->setPathInfo($noVdePath); return $this; }
/** * Validate and Match Blog Pages and modify request * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { $_identifier = trim($request->getPathInfo(), '/'); $pathInfo = explode('/', $_identifier); $blogRoute = $this->_url->getRoute(); if ($pathInfo[0] != $blogRoute) { return; } unset($pathInfo[0]); switch ($this->_url->getPermalinkType()) { case Url::PERMALINK_TYPE_DEFAULT: foreach ($pathInfo as $i => $route) { $pathInfo[$i] = $this->_url->getControllerName($route); } break; case Url::PERMALINK_TYPE_SHORT: if ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_SEARCH)) { $pathInfo[1] = Url::CONTROLLER_SEARCH; } elseif (count($pathInfo) == 1) { if ($this->_isArchiveIdentifier($pathInfo[1])) { $pathInfo[2] = $pathInfo[1]; $pathInfo[1] = Url::CONTROLLER_ARCHIVE; } elseif ($postId = $this->_getPostId($pathInfo[1])) { $pathInfo[2] = $pathInfo[1]; $pathInfo[1] = Url::CONTROLLER_POST; } elseif ($categoryId = $this->_getCategoryId($pathInfo[1])) { $pathInfo[2] = $pathInfo[1]; $pathInfo[1] = Url::CONTROLLER_CATEGORY; } } break; } $identifier = implode('/', $pathInfo); $condition = new \Magento\Framework\DataObject(['identifier' => $identifier, 'continue' => true]); $this->_eventManager->dispatch('magefan_blog_controller_router_match_before', ['router' => $this, 'condition' => $condition]); if ($condition->getRedirectUrl()) { $this->_response->setRedirect($condition->getRedirectUrl()); $request->setDispatched(true); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Redirect', ['request' => $request]); } if (!$condition->getContinue()) { return null; } $identifier = $condition->getIdentifier(); $success = false; $info = explode('/', $identifier); if (!$identifier) { $request->setModuleName('blog')->setControllerName('index')->setActionName('index'); $success = true; } elseif (count($info) > 1) { $store = $this->_storeManager->getStore()->getId(); switch ($info[0]) { case 'post': if (!($postId = $this->_getPostId($info[1]))) { return null; } $request->setModuleName('blog')->setControllerName('post')->setActionName('view')->setParam('id', $postId); $success = true; break; case 'category': if (!($categoryId = $this->_getCategoryId($info[1]))) { return null; } $request->setModuleName('blog')->setControllerName('category')->setActionName('view')->setParam('id', $categoryId); $success = true; break; case 'archive': $request->setModuleName('blog')->setControllerName('archive')->setActionName('view')->setParam('date', $info[1]); $success = true; break; case 'search': $request->setModuleName('blog')->setControllerName('search')->setActionName('index')->setParam('q', $info[1]); $success = true; break; case 'rss': $request->setModuleName('blog')->setControllerName('rss')->setActionName(isset($info[1]) ? $info[1] : 'index'); $success = true; break; } } if (!$success) { return null; } $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $_identifier); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', ['request' => $request]); }
/** * Validate and Match Cms Page and modify request * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { $helper = $this->_helper; if ($helper->getBlogConfig('general/enabled')) { $url_prefix = $helper->getBlogConfig('general/url_prefix'); $url_suffix = $helper->getBlogConfig('general/url_suffix'); if ($url_prefix == '') { return $this; } $path = trim($request->getPathInfo(), '/'); if (strpos($path, $url_prefix) === 0) { $array = explode('/', $path); if (count($array) == 1) { $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $path); $request->setPathInfo('/' . 'blog/post/index'); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); } elseif (count($array) == 2) { $url_key = $array[1]; $post = $this->_helper->getPostByUrl($url_key); if ($post && $post->getId()) { $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $url_key); $request->setPathInfo('/' . 'blog/post/view/id/' . $post->getId()); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); } } elseif (count($array) == 3) { $type = $array[1]; if ($type == 'post' && $array[2] == 'index') { if ($array[2] == 'index') { $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $path); $request->setPathInfo('/' . 'blog/post/index'); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); } else { $url_key = $array[2]; $post = $this->_helper->getPostByUrl($url_key); if ($post && $post->getId()) { $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $url_key); $request->setPathInfo('/' . 'blog/post/view/id/' . $post->getId()); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); } } } if ($type == 'post' && strpos($path, 'rss') !== false) { $path = str_replace($url_prefix, 'blog', $path); $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $path); $request->setPathInfo($path); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); } if ($type == 'topic') { $topicUrlKey = $array[2]; $topic = $this->_helper->getTopicByParam('url_key', $topicUrlKey); $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $path); $request->setPathInfo('/' . 'blog/topic/view/id/' . $topic->getId()); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); } if ($type == 'tag') { $tagUrlKey = $array[2]; $tag = $this->_helper->getTagByParam('url_key', $tagUrlKey); $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $path); $request->setPathInfo('/' . 'blog/tag/view/id/' . $tag->getId()); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); } if ($type == 'category') { $categoryName = $array[2]; $category = $this->_helper->getCategoryByParam('url_key', $categoryName); if ($category && $category->getId()) { $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $path); $request->setPathInfo('/' . 'blog/category/view/id/' . $category->getId()); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); } } } elseif (count($array) > 3) { if (strpos($path, 'rss') !== false) { $path = str_replace($url_prefix, 'blog', $path); $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $path); $request->setPathInfo($path); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); } } } } }
/** * Validate and Match Blog Pages and modify request * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { $_identifier = trim($request->getPathInfo(), '/'); if (strpos($_identifier, 'blog') !== 0) { return; } $identifier = str_replace(array('blog/', 'blog'), '', $_identifier); $condition = new \Magento\Framework\DataObject(['identifier' => $identifier, 'continue' => true]); $this->_eventManager->dispatch('magefan_blog_controller_router_match_before', ['router' => $this, 'condition' => $condition]); if ($condition->getRedirectUrl()) { $this->_response->setRedirect($condition->getRedirectUrl()); $request->setDispatched(true); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Redirect', ['request' => $request]); } if (!$condition->getContinue()) { return null; } $identifier = $condition->getIdentifier(); $success = false; $info = explode('/', $identifier); if (!$identifier) { $request->setModuleName('blog')->setControllerName('index')->setActionName('index'); $success = true; } elseif (count($info) > 1) { $store = $this->_storeManager->getStore()->getId(); switch ($info[0]) { case 'post': $post = $this->_postFactory->create(); $postId = $post->checkIdentifier($info[1], $this->_storeManager->getStore()->getId()); if (!$postId) { return null; } $request->setModuleName('blog')->setControllerName('post')->setActionName('view')->setParam('id', $postId); $success = true; break; case 'category': $category = $this->_categoryFactory->create(); $categoryId = $category->checkIdentifier($info[1], $this->_storeManager->getStore()->getId()); if (!$categoryId) { return null; } $request->setModuleName('blog')->setControllerName('category')->setActionName('view')->setParam('id', $categoryId); $success = true; break; case 'archive': $request->setModuleName('blog')->setControllerName('archive')->setActionName('view')->setParam('date', $info[1]); $success = true; break; case 'search': $request->setModuleName('blog')->setControllerName('search')->setActionName('index')->setParam('q', $info[1]); $success = true; break; } } if (!$success) { return null; } $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $_identifier); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', ['request' => $request]); }
/** * Perform custom url rewrites * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function rewrite(\Magento\Framework\App\RequestInterface $request = null) { if (!$this->_appState->isInstalled()) { return false; } if (is_null($this->getStoreId()) || false === $this->getStoreId()) { $this->setStoreId($this->_storeManager->getStore()->getId()); } /** * We have two cases of incoming paths - with and without slashes at the end ("/somepath/" and "/somepath"). * Each of them matches two url rewrite request paths - with and without slashes at the end * ("/somepath/" and "/somepath"). * Choose any matched rewrite, but in priority order that depends on same presence of slash and query params. */ $requestCases = array(); $pathInfo = $request->getPathInfo(); $origSlash = substr($pathInfo, -1) == '/' ? '/' : ''; $requestPath = trim($pathInfo, '/'); // If there were final slash - add nothing to less priority paths. And vice versa. $altSlash = $origSlash ? '' : '/'; $queryString = $this->_getQueryString(); // Query params in request, matching "path + query" has more priority if ($queryString) { $requestCases[] = $requestPath . $origSlash . '?' . $queryString; $requestCases[] = $requestPath . $altSlash . '?' . $queryString; } $requestCases[] = $requestPath . $origSlash; $requestCases[] = $requestPath . $altSlash; $this->loadByRequestPath($requestCases); $targetUrl = $request->getBaseUrl(); /** * Try to find rewrite by request path at first, if no luck - try to find by id_path */ if (!$this->getId() && isset($_GET['___from_store'])) { try { $fromStoreId = $this->_storeManager->getStore($_GET['___from_store'])->getId(); } catch (\Exception $e) { return false; } $this->setStoreId($fromStoreId)->loadByRequestPath($requestCases); if (!$this->getId()) { return false; } $currentStore = $this->_storeManager->getStore(); $this->setStoreId($currentStore->getId())->loadByIdPath($this->getIdPath()); $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear(); $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $currentStore->getCode(), $cookieMetadata); $targetUrl .= '/' . $this->getRequestPath(); $this->_sendRedirectHeaders($targetUrl, true); } if (!$this->getId()) { return false; } $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath()); $external = substr($this->getTargetPath(), 0, 6); $isPermanentRedirectOption = $this->hasOption('RP'); if ($external === 'http:/' || $external === 'https:') { $destinationStoreCode = $this->_storeManager->getStore($this->getStoreId())->getCode(); $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear(); $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $destinationStoreCode, $cookieMetadata); $this->_sendRedirectHeaders($this->getTargetPath(), $isPermanentRedirectOption); } else { $targetUrl .= '/' . $this->getTargetPath(); } $isRedirectOption = $this->hasOption('R'); $isStoreInUrl = $this->_scopeConfig->getValue(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if ($isRedirectOption || $isPermanentRedirectOption) { if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) { $targetUrl .= '/' . $storeCode . '/' . $this->getTargetPath(); } $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption); } if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) { $targetUrl .= '/' . $storeCode . '/' . $this->getTargetPath(); } $queryString = $this->_getQueryString(); if ($queryString) { $targetUrl .= '?' . $queryString; } $request->setRequestUri($targetUrl); $request->setPathInfo($this->getTargetPath()); return true; }
/** * @param RequestInterface $request * @return \Magento\Framework\App\ActionInterface */ public function match(RequestInterface $request) { $_brandHelper = $this->_brandHelper; if (!$this->dispatched) { $urlKey = trim($request->getPathInfo(), '/'); $origUrlKey = $urlKey; /** @var Object $condition */ $condition = new DataObject(['url_key' => $urlKey, 'continue' => true]); $this->eventManager->dispatch('ves_brand_controller_router_match_before', ['router' => $this, 'condition' => $condition]); $urlKey = $condition->getUrlKey(); if ($condition->getRedirectUrl()) { $this->response->setRedirect($condition->getRedirectUrl()); $request->setDispatched(true); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Redirect', ['request' => $request]); } if (!$condition->getContinue()) { return null; } $route = $_brandHelper->getConfig('general_settings/route'); if ($urlKey == $route) { $request->setModuleName('vesbrand')->setControllerName('index')->setActionName('index'); $request->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $urlKey); $this->dispatched = true; return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', ['request' => $request]); } $url_prefix = $_brandHelper->getConfig('general_settings/url_prefix'); $url_suffix = $_brandHelper->getConfig('general_settings/url_suffix'); $identifiers = explode('/', $urlKey); //Check Group Url if (count($identifiers) == 2 && $identifiers[0] == $url_prefix && strpos($identifiers[1], $url_suffix) || trim($url_prefix) == '' && count($identifiers) == 1) { $brandUrl = ''; if (trim($url_prefix) == '' && count($identifiers) == 1) { $brandUrl = str_replace($url_suffix, '', $identifiers[0]); } if (count($identifiers) == 2) { $brandUrl = str_replace($url_suffix, '', $identifiers[1]); } $group = $this->_groupCollection->getCollection()->addFieldToFilter('status', array('eq' => 1))->addFieldToFilter('url_key', array('eq' => $brandUrl))->getFirstItem(); if ($group && $group->getId()) { $request->setModuleName('vesbrand')->setControllerName('group')->setActionName('view')->setParam('group_id', $group->getId()); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $origUrlKey); $request->setDispatched(true); $this->dispatched = true; return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', ['request' => $request]); } } // Check Brand Url Key if (count($identifiers) == 2 && $identifiers[0] == $url_prefix && strpos($identifiers[1], $url_suffix) || trim($url_prefix) == '' && count($identifiers) == 1) { if (count($identifiers) == 2) { $brandUrl = str_replace($url_suffix, '', $identifiers[1]); } if (trim($url_prefix) == '' && count($identifiers) == 1) { $brandUrl = str_replace($url_suffix, '', $identifiers[0]); } $brand = $this->_brandCollection->getCollection()->addFieldToFilter('status', array('eq' => 1))->addFieldToFilter('url_key', array('eq' => $brandUrl))->getFirstItem(); if ($brand && $brand->getId() && (in_array($this->storeManager->getStore()->getId(), $brand->getStoreId()) || in_array(0, $brand->getStoreId()))) { $request->setModuleName('vesbrand')->setControllerName('brand')->setActionName('view')->setParam('brand_id', $brand->getId()); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $origUrlKey); $request->setDispatched(true); $this->dispatched = true; return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', ['request' => $request]); } } } }
/** * Validate and Match * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { /* * We will search “examplerouter” and “exampletocms” words and make forward depend on word * -examplerouter will forward to base router to match inchootest front name, test controller path and test controller class * -exampletocms will set front name to cms, controller path to page and action to view */ // $identifier = trim($request->getPathInfo(), '/'); // // if(strpos($identifier, 'exampletocms') !== false) { // return; // /* // * We must set module, controller path and action name + we will set page id 5 witch is about us page on // * default magento 2 installation with sample data. // */ // $request->setModuleName('cms')->setControllerName('page')->setActionName('view')->setParam('page_id', 5); // } else if(strpos($identifier, 'examplerouter') !== false) { // /* // * We must set module, controller path and action name for our controller class(Controller/Test/Test.php) // */ // $request->setModuleName('shopbybrand')->setControllerName('view')->setActionName('index'); // } else { // //There is no match // return; // } // // /* // * We have match and now we will forward action // */ // return $this->actionFactory->create( // 'Magento\Framework\App\Action\Forward', // ['request' => $request] // ); $identifier = trim($request->getPathInfo(), '/'); if (strpos($identifier, 'brand/view/index/id') !== false) { // called via id return null; } else { if (strpos($identifier, 'brand/') !== false) { $patharr = explode("/", $identifier); $urlpath = end($patharr); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $modelcollection = $objectManager->get('\\Emizentech\\ShopByBrand\\Model\\BrandFactory')->create()->getCollection(); $modelcollection->addFieldToFilter('url_key', $urlpath); if ($modelcollection->count() >= 1 && ($brand = $modelcollection->getFirstItem())) { // var_dump($brand->debug()); $request->setModuleName('brand')->setControllerName('view')->setActionName('index')->setParam('id', $brand->getId()); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier); $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, '/' . $identifier); $request->setPathInfo('/' . $identifier); return; } else { // not found any URL Key return null; } } else { //There is no match return null; } } }