diff --git a/Controller/Router.php b/Controller/Router.php
index 44870edc..13f51a22 100755
--- a/Controller/Router.php
+++ b/Controller/Router.php
@@ -155,149 +155,6 @@ public function match(\Magento\Framework\App\RequestInterface $request)
if (!$this->config->isEnabled()) {
return null;
}
- /*
- $_identifier = trim($request->getPathInfo(), '/');
- $_identifier = urldecode($_identifier);
-
- $pathInfo = explode('/', $_identifier);
- $blogRoute = $this->_url->getRoute();
-
- if ($pathInfo[0] != $blogRoute) {
- return;
- }
-
- unset($pathInfo[0]);
-
- if (!count($pathInfo)) {
- $request
- ->setRouteName('blog')
- ->setControllerName('index')
- ->setActionName('index');
- } elseif ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_RSS)) {
- if (!isset($pathInfo[2]) || in_array($pathInfo[2], ['index', 'feed'])) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_RSS)
- ->setActionName('feed');
- }
- } elseif ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_SEARCH)
- && !empty($pathInfo[2])
- ) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_SEARCH)
- ->setActionName('index')
- ->setParam('q', $pathInfo[2]);
- } elseif ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_AUTHOR)
- && !empty($pathInfo[2])
- && ($authorId = $this->_getAuthorId($pathInfo[2]))
- ) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_AUTHOR)
- ->setActionName('view')
- ->setParam('id', $authorId);
- } elseif ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_TAG)
- && !empty($pathInfo[2])
- && $tagId = $this->_getTagId($pathInfo[2])
- ) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_TAG)
- ->setActionName('view')
- ->setParam('id', $tagId);
- } else {
- $controllerName = null;
- if (Url::PERMALINK_TYPE_DEFAULT == $this->_url->getPermalinkType()) {
- $controllerName = $this->_url->getControllerName($pathInfo[1]);
- unset($pathInfo[1]);
- }
-
- $pathInfo = array_values($pathInfo);
- $pathInfoCount = count($pathInfo);
-
- if ($pathInfoCount == 1) {
- if ((!$controllerName || $controllerName == Url::CONTROLLER_ARCHIVE)
- && $this->_isArchiveIdentifier($pathInfo[0])
- ) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_ARCHIVE)
- ->setActionName('view')
- ->setParam('date', $pathInfo[0]);
- } elseif ((!$controllerName || $controllerName == Url::CONTROLLER_POST)
- && $postId = $this->_getPostId($pathInfo[0])
- ) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_POST)
- ->setActionName('view')
- ->setParam('id', $postId);
- } elseif ((!$controllerName || $controllerName == Url::CONTROLLER_CATEGORY)
- && $categoryId = $this->_getCategoryId($pathInfo[0])
- ) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_CATEGORY)
- ->setActionName('view')
- ->setParam('id', $categoryId);
- }
- } elseif ($pathInfoCount > 1) {
- $postId = 0;
- $categoryId = 0;
- $first = true;
- $pathExist = true;
-
- for ($i = $pathInfoCount - 1; $i >= 0; $i--) {
- if ((!$controllerName || $controllerName == Url::CONTROLLER_POST)
- && $first
- && ($postId = $this->_getPostId($pathInfo[$i]))
- ) {
- //we have postId
- } elseif ((!$controllerName || !$first || $controllerName == Url::CONTROLLER_CATEGORY)
- && ($cid = $this->_getCategoryId($pathInfo[$i], $first))
- ) {
- if (!$categoryId) {
- $categoryId = $cid;
- }
- } else {
- $pathExist = false;
- break;
- }
-
- if ($first) {
- $first = false;
- }
- }
- if ($pathExist) {
- if ($postId) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_POST)
- ->setActionName('view')
- ->setParam('id', $postId);
- if ($categoryId) {
- $request->setParam('category_id', $categoryId);
- }
- } elseif ($categoryId) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_CATEGORY)
- ->setActionName('view')
- ->setParam('id', $categoryId);
- }
- } elseif ((!$controllerName || $controllerName == Url::CONTROLLER_POST)
- && $postId = $this->_getPostId(implode('/', $pathInfo))
- ) {
- $request
- ->setRouteName('blog')
- ->setControllerName(Url::CONTROLLER_POST)
- ->setActionName('view')
- ->setParam('id', $postId);
- }
- }
- }
- */
$pathInfo = $request->getPathInfo();
$_identifier = trim($pathInfo, '/');
@@ -382,116 +239,4 @@ public function match(\Magento\Framework\App\RequestInterface $request)
['request' => $request]
);
}
-
- /**
- * Retrieve post id by identifier
- * @param string $identifier
- * @return int
- * @deprecated Use URL resolver interface instead
- */
- protected function _getPostId($identifier, $checkSufix = true)
- {
- return $this->getObjectId(
- $this->_postFactory,
- Url::CONTROLLER_POST,
- $identifier,
- $checkSufix
- );
- }
-
- /**
- * Retrieve category id by identifier
- * @param string $identifier
- * @return int
- * @deprecated Use URL resolver interface instead
- */
- protected function _getCategoryId($identifier, $checkSufix = true)
- {
- return $this->getObjectId(
- $this->_categoryFactory,
- Url::CONTROLLER_CATEGORY,
- $identifier,
- $checkSufix
- );
- }
-
- /**
- * Retrieve category id by identifier
- * @param string $identifier
- * @param bool $checkSufix
- * @return int
- * @deprecated Use URL resolver interface instead
- */
- protected function _getAuthorId($identifier, $checkSufix = true)
- {
- return $this->getObjectId(
- $this->_authorFactory,
- Url::CONTROLLER_AUTHOR,
- $identifier,
- $checkSufix
- );
- }
-
- /**
- * Retrieve tag id by identifier
- * @param string $identifier
- * @param bool $checkSufix
- * @return int
- * @deprecated Use URL resolver interface instead
- */
- protected function _getTagId($identifier, $checkSufix = true)
- {
- return $this->getObjectId(
- $this->_tagFactory,
- Url::CONTROLLER_TAG,
- $identifier,
- $checkSufix
- );
- }
-
- /**
- * @param $factory
- * @param string $controllerName
- * @param string $identifier
- * @param bool $checkSufix
- * @return mixed
- * @deprecated Use URL resolver interface instead
- */
- protected function getObjectId($factory, $controllerName, $identifier, $checkSufix)
- {
- $key = $controllerName . '-' .$identifier . ($checkSufix ? '-checksufix' : '');
- if (!isset($this->ids[$key])) {
- $sufix = $this->_url->getUrlSufix($controllerName);
-
- $trimmedIdentifier = $this->_url->trimSufix($identifier, $sufix);
-
- if ($checkSufix && $sufix && $trimmedIdentifier == $identifier) { //if url without sufix
- $this->ids[$key] = 0;
- } else {
- $object = $factory->create();
- $this->ids[$key] = $object->checkIdentifier(
- $trimmedIdentifier,
- $this->_storeManager->getStore()->getId()
- );
- }
- }
-
- return $this->ids[$key];
- }
-
- /**
- * Detect arcive identifier
- * @param string $identifier
- * @return boolean
- * @deprecated Use URL resolver interface instead
- */
- protected function _isArchiveIdentifier($identifier)
- {
- $info = explode('-', $identifier);
- return count($info) == 2
- && strlen($info[0]) == 4
- && strlen($info[1]) == 2
- && is_numeric($info[0])
- && is_numeric($info[1]);
- }
}
diff --git a/Controller/RouterAfter.php b/Controller/RouterAfter.php
new file mode 100644
index 00000000..823f319f
--- /dev/null
+++ b/Controller/RouterAfter.php
@@ -0,0 +1,233 @@
+actionFactory = $actionFactory;
+ $this->response = $response;
+ $this->storeManager = $storeManager;
+ $this->eventManager = $eventManager;
+ $this->postFactory = $postFactory;
+ $this->categoryFactory = $categoryFactory;
+ $this->authorFactory = $authorFactory;
+ $this->tagFactory = $tagFactory;
+ $this->blogUrl = $blogUrl;
+ $this->urlResolver = $urlResolver ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
+ \Magefan\Blog\Api\UrlResolverInterface::class
+ );
+ $this->config = $config ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
+ Config::class
+ );
+ }
+
+
+ /**
+ * @param \Magento\Framework\App\RequestInterface $request
+ * @return \Magento\Framework\App\ActionInterface|null
+ * @throws \Magento\Framework\Exception\LocalizedException
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
+ */
+ public function match(\Magento\Framework\App\RequestInterface $request)
+ {
+ if (!$this->config->isEnabled()) {
+ return null;
+ }
+ $_identifier = trim($request->getPathInfo(), '/');
+ $_identifier = urldecode($_identifier);
+
+ $storeId = $this->storeManager->getStore()->getId();
+
+ $stores = [];
+ foreach ($this->storeManager->getStores() as $store) {
+ if (!$store->isActive()) {
+ continue;
+ }
+ $stores[] = $store->getId();
+ }
+
+ foreach ($stores as $_storeId) {
+ if ($_storeId == $storeId) {
+ continue;
+ }
+ $blogUrl = $this->blogUrl;
+ $_originStoreId = $blogUrl->getStoreId();
+ $blogUrl->setStoreId($_storeId);
+ $blogPage = $this->getBlogPage($_identifier);
+ $blogUrl->setStoreId($_originStoreId);
+
+ if (!$blogPage || empty($blogPage['type']) || empty($blogPage['id'])) {
+ continue;
+ }
+ $redirectUrl = null;
+ switch ($blogPage['type']) {
+ case Url::CONTROLLER_INDEX:
+ $redirectUrl = $blogUrl->getBaseUrl();
+ break;
+ case Url::CONTROLLER_TAG:
+ $blogPage['type'] = 'tag';
+ $tag = $this->tagFactory->create()->load($blogPage['id']);
+ if ($tag->getId()) {
+ $redirectUrl = $tag->getTagUrl();
+ }
+ break;
+ case Url::CONTROLLER_AUTHOR:
+ $blogPage['type'] = 'author';
+ $author = $this->authorFactory->create()->load($blogPage['id']);
+ if ($author->getId()) {
+ $redirectUrl = $author->getAuthorUrl();
+ }
+ break;
+ case Url::CONTROLLER_RSS:
+ $redirectUrl = $blogUrl->getUrl(
+ $blogPage['id'],
+ $blogUrl::CONTROLLER_RSS
+ );
+ break;
+ case Url::CONTROLLER_SEARCH:
+ $redirectUrl = $blogUrl->getUrl(
+ $blogPage['id'],
+ $blogUrl::CONTROLLER_SEARCH
+ );
+ break;
+ case Url::CONTROLLER_ARCHIVE:
+ $redirectUrl = $blogUrl->getUrl(
+ $blogPage['id'],
+ $blogUrl::CONTROLLER_ARCHIVE
+ );
+ break;
+ case Url::CONTROLLER_POST:
+ case Url::CONTROLLER_CATEGORY:
+ $redirectId = $blogPage['id'];
+
+ if ($blogPage['type'] == Url::CONTROLLER_POST) {
+ $post = $this->postFactory->create()->load($redirectId);
+ if ($post->isVisibleOnStore($_originStoreId)) {
+ if ($post->isVisibleOnStore($storeId)) {
+ $redirectUrl = $post->getPostUrl();
+ } else {
+ $redirectUrl = $blogUrl->getBaseUrl();
+ }
+ }
+ } elseif ($blogPage['type'] == Url::CONTROLLER_CATEGORY) {
+ $category = $this->categoryFactory->create()->load($redirectId);
+ if ($category->isVisibleOnStore($_originStoreId)) {
+ if ($category->isVisibleOnStore($storeId)) {
+ $redirectUrl = $category->getCategoryUrl();
+ } else {
+ $redirectUrl = $blogUrl->getBaseUrl();
+ }
+ }
+ }
+ break;
+ }
+
+ if ($redirectUrl) {
+ $this->response->setRedirect($redirectUrl, 301);
+ $request->setDispatched(true);
+ return $this->actionFactory->create(
+ \Magento\Framework\App\Action\Redirect::class,
+ ['request' => $request]
+ );
+ }
+ }
+ }
+
+ /**
+ * @param $_identifier
+ * @return array|void|null
+ */
+ protected function getBlogPage($_identifier)
+ {
+ $urlResolver = $this->urlResolver;
+ $urlResolver->setStoreId($this->blogUrl->getStoreId());
+ return $urlResolver->resolve($_identifier);
+ }
+}
diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml
index b33e42b0..a7f71ecd 100755
--- a/etc/frontend/di.xml
+++ b/etc/frontend/di.xml
@@ -16,6 +16,11 @@
- false
- 60
+ -
+
- Magefan\Blog\Controller\RouterAfter
+ - false
+ - 61
+