Skip to content

Commit e46fa7e

Browse files
committed
Added suffix for tags and authors URLs
1 parent 656ff62 commit e46fa7e

File tree

3 files changed

+75
-71
lines changed

3 files changed

+75
-71
lines changed

Controller/Router.php

Lines changed: 61 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,9 @@ class Router implements \Magento\Framework\App\RouterInterface
8484
protected $_response;
8585

8686
/**
87-
* @var array
87+
* @var array;
8888
*/
89-
protected $_postId;
90-
91-
/**
92-
* @var array
93-
*/
94-
protected $_categoryId;
95-
96-
/**
97-
* @var int
98-
*/
99-
protected $_authorId;
100-
101-
/**
102-
* @var int
103-
*/
104-
protected $_tagId;
89+
protected $ids;
10590

10691
/**
10792
* @param \Magento\Framework\App\ActionFactory $actionFactory
@@ -125,7 +110,8 @@ public function __construct(
125110
\Magefan\Blog\Model\TagFactory $tagFactory,
126111
\Magento\Store\Model\StoreManagerInterface $storeManager,
127112
\Magento\Framework\App\ResponseInterface $response
128-
) {
113+
)
114+
{
129115
$this->actionFactory = $actionFactory;
130116
$this->_eventManager = $eventManager;
131117
$this->_url = $url;
@@ -330,22 +316,12 @@ public function match(\Magento\Framework\App\RequestInterface $request)
330316
*/
331317
protected function _getPostId($identifier, $checkSufix = true)
332318
{
333-
$key = $identifier . ($checkSufix ? '-checksufix' : '');
334-
if (!isset($this->_postId[$key])) {
335-
$sufix = $this->_url->getUrlSufix(Url::CONTROLLER_POST);
336-
$trimmedIdentifier = $this->_url->trimSufix($identifier, $sufix);
337-
if ($checkSufix && $sufix && $trimmedIdentifier == $identifier) { //if url without sufix
338-
$this->_postId[$key] = 0;
339-
} else {
340-
$post = $this->_postFactory->create();
341-
$this->_postId[$key] = $post->checkIdentifier(
342-
$trimmedIdentifier,
343-
$this->_storeManager->getStore()->getId()
344-
);
345-
}
346-
}
347-
348-
return $this->_postId[$key];
319+
return $this->getObjectId(
320+
$this->_postFactory,
321+
Url::CONTROLLER_POST,
322+
$identifier,
323+
$checkSufix
324+
);
349325
}
350326

351327
/**
@@ -355,58 +331,73 @@ protected function _getPostId($identifier, $checkSufix = true)
355331
*/
356332
protected function _getCategoryId($identifier, $checkSufix = true)
357333
{
358-
$key = $identifier . ($checkSufix ? '-checksufix' : '');
359-
if (!isset($this->_categoryId[$key])) {
360-
$sufix = $this->_url->getUrlSufix(Url::CONTROLLER_CATEGORY);
361-
362-
$trimmedIdentifier = $this->_url->trimSufix($identifier, $sufix);
363-
364-
if ($checkSufix && $sufix && $trimmedIdentifier == $identifier) { //if url without sufix
365-
$this->_categoryId[$key] = 0;
366-
} else {
367-
$category = $this->_categoryFactory->create();
368-
$this->_categoryId[$key] = $category->checkIdentifier(
369-
$trimmedIdentifier,
370-
$this->_storeManager->getStore()->getId()
371-
);
372-
}
373-
}
374-
375-
return $this->_categoryId[$key];
334+
return $this->getObjectId(
335+
$this->_categoryFactory,
336+
Url::CONTROLLER_CATEGORY,
337+
$identifier,
338+
$checkSufix
339+
);
376340
}
377341

378342
/**
379343
* Retrieve category id by identifier
380-
* @param string $identifier
344+
* @param string $identifier
345+
* @param bool $checkSufix
381346
* @return int
382347
*/
383-
protected function _getAuthorId($identifier)
348+
protected function _getAuthorId($identifier, $checkSufix = true)
384349
{
385-
if (is_null($this->_authorId)) {
386-
$author = $this->_authorFactory->create();
387-
$this->_authorId = $author->checkIdentifier(
388-
$identifier
389-
);
390-
}
391-
392-
return $this->_authorId;
350+
return $this->getObjectId(
351+
$this->_authorFactory,
352+
Url::CONTROLLER_AUTHOR,
353+
$identifier,
354+
$checkSufix
355+
);
393356
}
394357

395358
/**
396359
* Retrieve tag id by identifier
397-
* @param string $identifier
360+
* @param string $identifier
361+
* @param bool $checkSufix
398362
* @return int
399363
*/
400-
protected function _getTagId($identifier)
364+
protected function _getTagId($identifier, $checkSufix = true)
401365
{
402-
if (is_null($this->_tagId)) {
403-
$tag = $this->_tagFactory->create();
404-
$this->_tagId = $tag->checkIdentifier(
405-
$identifier
406-
);
366+
return $this->getObjectId(
367+
$this->_tagFactory,
368+
Url::CONTROLLER_TAG,
369+
$identifier,
370+
$checkSufix
371+
);
372+
}
373+
374+
/**
375+
* @param $factory
376+
* @param string $controllerName
377+
* @param string $identifier
378+
* @param bool $checkSufix
379+
* @return mixed
380+
*/
381+
protected function getObjectId($factory, $controllerName, $identifier, $checkSufix)
382+
{
383+
$key = $controllerName . '-' .$identifier . ($checkSufix ? '-checksufix' : '');
384+
if (!isset($this->ids[$key])) {
385+
$sufix = $this->_url->getUrlSufix($controllerName);
386+
387+
$trimmedIdentifier = $this->_url->trimSufix($identifier, $sufix);
388+
389+
if ($checkSufix && $sufix && $trimmedIdentifier == $identifier) { //if url without sufix
390+
$this->ids[$key] = 0;
391+
} else {
392+
$object = $factory->create();
393+
$this->ids[$key] = $object->checkIdentifier(
394+
$trimmedIdentifier,
395+
$this->_storeManager->getStore()->getId()
396+
);
397+
}
407398
}
408399

409-
return $this->_tagId;
400+
return $this->ids[$key];
410401
}
411402

412403
/**

Model/Url.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,12 @@ protected function getExpandedItentifier($identifier)
265265
*/
266266
protected function addUrlSufix($url, $controllerName)
267267
{
268-
if (in_array($controllerName, [self::CONTROLLER_POST, self::CONTROLLER_CATEGORY])) {
268+
if (in_array($controllerName, [
269+
self::CONTROLLER_POST,
270+
self::CONTROLLER_CATEGORY,
271+
self::CONTROLLER_AUTHOR,
272+
self::CONTROLLER_TAG
273+
])) {
269274
if ($sufix = $this->getUrlSufix($controllerName)) {
270275
$char = false;
271276
foreach (['#', '?'] as $ch) {

etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,18 @@
387387
<label>Tag Route</label>
388388
<comment>E.g.: "tag" will make the blog posts by tag accessible from mystore.com/{blog_route}/tag/tag-name/</comment>
389389
</field>
390+
<field id="tag_sufix" translate="label comment" type="text" sortOrder="56" showInDefault="1" showInWebsite="1" showInStore="1">
391+
<label>Tag URL Suffix</label>
392+
<comment>E.g.: ".html" will make the blog tag accessible from mystore.com/{blog_route}/tag/tag-name.html</comment>
393+
</field>
390394
<field id="author_route" translate="label comment" type="text" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
391395
<label>Author Route</label>
392396
<comment>E.g.: "author" will make the blog author posts accessible from mystore.com/{blog_route}/author/author-name/</comment>
393397
</field>
398+
<field id="author_sufix" translate="label comment" type="text" sortOrder="61" showInDefault="1" showInWebsite="1" showInStore="1">
399+
<label>Author URL Suffix</label>
400+
<comment>E.g.: ".html" will make the blog author accessible from mystore.com/{blog_route}/author/author-name.html</comment>
401+
</field>
394402
<field id="search_route" translate="label comment" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
395403
<label>Search Route</label>
396404
<comment>E.g.: "search" will make the blog search accessible from mystore.com/{blog_route}/search/query/</comment>

0 commit comments

Comments
 (0)