public function match(RequestInterface $request)
 {
     $match = preg_match('#^\\/example-custom-route-handler-([0-9]*)$#', $request->getPathInfo());
     if ($match) {
         $request->setPathInfo('/cms');
         return $this->_actionFactory->create('Magento\\Framework\\App\\Action\\Forward');
     }
 }
Example #2
0
 /**
  * use /test-action-config as a test url for example
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @return \Magento\Framework\App\ActionInterface|null
  */
 public function match(\Magento\Framework\App\RequestInterface $request)
 {
     $info = $request->getPathInfo();
     if (preg_match("%^/(test)-(.*?)-(.*?)\$%", $info, $m)) {
         $request->setPathInfo(sprintf("/%s/%s/%s", $m[1], $m[2], $m[3]));
         return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', ['request' => $request]);
     }
     return null;
 }
Example #3
0
 /**
  * Validate and Match Cms Page and modify request
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @return mixed
  */
 public function match(\Magento\Framework\App\RequestInterface $request)
 {
     $identifier = trim($request->getPathInfo(), '/');
     $urlRewrite = $this->urlMatcher->match($identifier, $this->storeManager->getStore()->getId());
     if ($urlRewrite === null) {
         return null;
     }
     $redirectType = $urlRewrite->getRedirectType();
     if ($redirectType) {
         $redirectCode = $redirectType == \Magento\UrlRedirect\Model\OptionProvider::PERMANENT ? 301 : 302;
         $this->response->setRedirect($urlRewrite->getTargetPath(), $redirectCode);
         $request->setDispatched(true);
         return $this->actionFactory->createController('Magento\\Framework\\App\\Action\\Redirect', array('request' => $request));
     }
     $request->setPathInfo('/' . $urlRewrite->getTargetPath());
     return $this->actionFactory->createController('Magento\\Framework\\App\\Action\\Forward', array('request' => $request));
 }
Example #4
0
 /**
  * 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 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');
                 }
             }
         }
     }
 }
Example #6
0
 /**
  * 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;
 }
 /**
  * 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;
         }
     }
 }