예제 #1
0
 /**
  * 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\DataObject(['identifier' => $identifier, 'continue' => true]);
     $this->_eventManager->dispatch('cms_controller_router_match_before', ['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');
     }
     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');
 }
 /**
  * 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]);
 }
예제 #3
0
 /**
  * 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]);
 }