This blog will help you to create seo friendly url with your custom module. We’ll learn how to create seo friendly url and change your id with custom name on url.
Files:
app/code/Bluethink/ModuleName/etc/di.xml
app/code/Bluethink/ModuleName/Controller/Router.php
app/code/Bluethink/ModuleName/Helper/Data.php
app/code/Bluethink/ModuleName/view/frontend/templates/view.phtml
app/code/Bluethink/ModuleName/Block/View.php
Step 1: Create file di.xml
app/code/Bluethink/ModuleName/etc/di.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\App\RouterList"> <arguments> <argument name="routerList" xsi:type="array"> <item name="faqrouter" xsi:type="array"> <item name="class" xsi:type="string">Bluethink\Faq\Controller\Router</item> <item name="disable" xsi:type="boolean">false</item> <item name="sortOrder" xsi:type="string">22</item> </item> </argument> </arguments> </type> </config> |
Step 2:Create Controller Router.php
app/code/Bluethink/ModuleName/Controller/Router.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<?php namespace Bluethink\Faq\Controller; use Magento\Framework\App\ActionFactory; use Magento\Framework\App\ResponseInterface; use Magento\Framework\App\RequestInterface; use Magento\Framework\App\ActionInterface; use Bluethink\Faq\Model\Category; use Bluethink\Faq\Model\Faq; class Router implements \Magento\Framework\App\RouterInterface { /** @var ActionFactory */ protected $actionFactory; /** @var ResponseInterface */ protected $_response; /** * @param \Magento\Framework\App\ActionFactory $actionFactory * @param \Magento\Framework\App\ResponseInterface $response * @param \Bluethink\Faq\Model\Category $category * @param \Bluethink\Faq\Model\Faq $faq */ public function __construct( ActionFactory $actionFactory, ResponseInterface $response, Category $category, Faq $faq ) { $this->actionFactory = $actionFactory; $this->_response = $response; $this->category = $category; $this->faq = $faq; } /** * Redirect Seo Friendly Url * * @param RequestInterface $request */ public function match(RequestInterface $request): ?ActionInterface { $identifier = trim($request->getPathInfo(), '/'); $id = ''; if (strpos($identifier, 'category') !== false) { $finalKey = explode('/', $identifier); $urlKey = end($finalKey); $categoryModel = $this->category->load($urlKey, 'url_key'); if ($categoryModel->getId()) { $id = $categoryModel->getId(); } if ($id) { $request->setModuleName('faq')-> //module name setControllerName('view')-> //controller name setActionName('index')-> //action name setParams([ 'id' => $id ]); //custom parameters } } if (strpos($identifier, 'article') !== false) { $finalKey = explode('/', $identifier); $urlKey = end($finalKey); $faqModel = $this->faq->load($urlKey, 'url_key'); if ($faqModel->getId()) { $id = $faqModel->getId(); } if ($id) { $request->setModuleName('faq')-> //module name setControllerName('index')-> //controller name setActionName('article')-> //action name setParams([ 'id' => $id ]); //custom parameters } } return null; } } |
Step 3: Create Helper file Data.php
app/code/Bluethink/ModuleName/Helper/Data.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace Bluethink\Faq\Helper; use \Magento\Framework\App\Helper\AbstractHelper; class Data extends AbstractHelper { /** * Set seo url * * @param int $id * @param string $url */ public function getSeoUrl($id, $url) { $valueFromConfig = $this->scopeConfig->getValue( 'faqs/general/seourl', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, ); if ($valueFromConfig) { // check seo enabled from admin panel return 'faq/article/'.$url; // seo url } return 'faq/index/article/id/'.$id; // default url } } |
Step 4: Create View file view.phtml
app/code/Bluethink/ModuleName/view/frontend/templates/view.phtml
1 2 3 |
<a href="<?=$block->escapeUrl($block->getUrl($block->getSeoUrl($data->getFaqId(),$data->getUrlKey())))?>"> <?=$block->escapeHtmlAttr($data->getName()) ?> </a> |
Step 5: Create Blcok file View.php
app/code/Bluethink/ModuleName/Block/View.php
1 2 3 4 |
public function getSeoUrl($id,$urlKey) { return $this->helper->getSeoUrl($id, $urlKey); } Database : |
It is very helpfull.
2022-12-14 at 1:18 pm