Skip to content

Commit 9df4309

Browse files
committed
Fixed issues in last commit
1 parent c52c5ea commit 9df4309

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

Controller/Router.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -155,32 +155,39 @@ public function match(\Magento\Framework\App\RequestInterface $request)
155155
unset($pathInfo[0]);
156156

157157
if (!count($pathInfo)) {
158-
$request->setControllerName('index')->setActionName('index');
158+
$request
159+
->setModuleName('blog')
160+
->setControllerName('index')
161+
->setActionName('index');
159162
} elseif ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_RSS)) {
160163
$request
161164
->setModuleName('blog')
162165
->setControllerName(Url::CONTROLLER_RSS)
163-
->setActionName(isset($info[2]) ? $info[2] : 'index');
164-
} elseif ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_SEARCH)) {
166+
->setActionName(isset($pathInfo[2]) ? $pathInfo[2] : 'index');
167+
} elseif ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_SEARCH)
168+
&& !empty($pathInfo[2])
169+
) {
165170
$request
166171
->setModuleName('blog')
167172
->setControllerName(Url::CONTROLLER_SEARCH)
168173
->setActionName('index')
169-
->setParams('q', $pathInfo[2]);
174+
->setParam('q', $pathInfo[2]);
170175
} elseif ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_AUTHOR)
171-
&& ($authorId = $this->_getAuthorId($info[2]))
176+
&& !empty($pathInfo[2])
177+
&& ($authorId = $this->_getAuthorId($pathInfo[2]))
172178
) {
173179
$request
174180
->setModuleName('blog')
175181
->setControllerName(Url::CONTROLLER_AUTHOR)
176182
->setActionName('view')
177-
->setParam('id', $$authorId);
183+
->setParam('id', $authorId);
178184
} elseif ($pathInfo[1] == $this->_url->getRoute(Url::CONTROLLER_TAG)
179-
&& $tagId = $this->_getTagId($info[2])
185+
&& !empty($pathInfo[2])
186+
&& $tagId = $this->_getTagId($pathInfo[2])
180187
) {
181188
$request
182189
->setModuleName('blog')
183-
->setControllerName(Url::CONTROLLER_AUTHOR)
190+
->setControllerName(Url::CONTROLLER_TAG)
184191
->setActionName('view')
185192
->setParam('id', $tagId);
186193
} else {

Model/Category.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public function getCategoryUrl()
319319
*/
320320
public function getCanonicalUrl()
321321
{
322-
return $this->_url->getUrl($this->getIdentifier(), $this->controllerName);
322+
return $this->_url->getCanonicalUrl($this);
323323
}
324324

325325
/**

Model/Post.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public function getPostUrl()
266266
*/
267267
public function getCanonicalUrl()
268268
{
269-
return $this->_url->getUrl($this->getIdentifier(), $this->controllerName);
269+
return $this->_url->getCanonicalUrl($this);
270270
}
271271

272272
/**

Model/Url.php

+50-4
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,56 @@ public function getUrl($identifier, $controllerName)
152152
'_direct' => $this->getUrlPath($identifier, $controllerName)
153153
]);
154154

155-
$url = trim($url, '/');
155+
return $url;
156+
}
157+
158+
/**
159+
* Retrieve canonical url
160+
* @param \Magento\Framework\Model\AbstractModel $object
161+
* @return string
162+
*/
163+
public function getCanonicalUrl(\Magento\Framework\Model\AbstractModel $object)
164+
{
165+
$storeIds = $object->getStoreIds();
166+
$useDefaultStore = false;
167+
$currentStore = $this->_storeManager->getStore($object->getStoreId());
168+
169+
if (is_array($storeIds)) {
170+
if (0 == array_values($storeIds)[0]) {
171+
$useDefaultStore = true;
172+
} elseif (count($storeIds > 1)) {
173+
foreach ($storeIds as $storeId) {
174+
if ($storeId != $currentStore->getId()) {
175+
$store = $this->_storeManager->getStore($storeId);
176+
if ($store->getGroupId() == $currentStore->getGroupId()) {
177+
$useDefaultStore = true;
178+
break;
179+
}
180+
}
181+
}
182+
}
183+
}
184+
185+
$storeChanged = false;
186+
if ($useDefaultStore) {
187+
$newStore = $currentStore->getGroup()->getDefaultStore();
188+
$origStore = $this->_url->getScope();
189+
if ($newStore->getId() != $origStore->getId()) {
190+
$this->_url->setScope($newStore);
191+
$storeChanged = true;
192+
}
193+
}
194+
195+
$url = $this->getUrl($object->getIdentifier(), $object->getControllerName());
196+
197+
if ($storeChanged) {
198+
$this->_url->setScope($origStore);
199+
}
156200

157201
return $url;
158202
}
159203

204+
160205
/**
161206
* Retrieve blog url path
162207
* @param string $identifier
@@ -168,16 +213,16 @@ public function getUrlPath($identifier, $controllerName)
168213
$identifier = $this->getExpandedItentifier($identifier);
169214
switch ($this->getPermalinkType()) {
170215
case self::PERMALINK_TYPE_DEFAULT :
171-
$path = $this->getRoute() . '/' . $this->getRoute($controllerName) . '/' . $identifier;
216+
$path = $this->getRoute() . '/' . $this->getRoute($controllerName) . '/' . $identifier . ( $identifier ? '/' : '');
172217
break;
173218
case self::PERMALINK_TYPE_SHORT :
174219
if ($controllerName == self::CONTROLLER_SEARCH
175220
|| $controllerName == self::CONTROLLER_AUTHOR
176221
|| $controllerName == self::CONTROLLER_TAG
177222
) {
178-
$path = $this->getRoute() . '/' . $this->getRoute($controllerName) . '/' . $identifier;
223+
$path = $this->getRoute() . '/' . $this->getRoute($controllerName) . '/' . $identifier . ( $identifier ? '/' : '');
179224
} else {
180-
$path = $this->getRoute() . '/' . $identifier;
225+
$path = $this->getRoute() . '/' . $identifier . ( $identifier ? '/' : '');
181226
}
182227
break;
183228
}
@@ -297,4 +342,5 @@ protected function _getConfig($key)
297342
);
298343
}
299344

345+
300346
}

0 commit comments

Comments
 (0)