Step 1:
Create a custom module based on Magento 2 structure in the folder app/code.
Ex: Magento 2 root directory/app/code/Dckap/Newlink/
Step 2:
Create module.xml file in the below path to define your Magento 2 extension.
Magento 2 root directory/app/code/Dckap/Newlink/etc/module.xml
Code:
<?xml version=“1.0”?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Dckap_Newlink" setup_version="2.0.1"></module>
</config>
Step 3:
Create routes.xml in the below path for your custom link.
Magento 2 root directory/app/Dckap/Newlink/etc/frontend/routes.xml
Code:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="newlink" frontName="newlink">
<module name="Dckap_Newlink" />
</route>
</router>
</config>
Step 4:
Create layout file in the below path for a custom header.
Magento2 root directory/app/Dckap/Newlink/view/frontend/layout/header.xml
Code:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="header.links">
<move element="test-last" destination="header.links"/>
<block class="Dckap\Newlink\Block\Header" name="test-link" after="my-account-link"/>
</referenceBlock>
</body>
</page>
Step 5:
Create block file that is referred in the layout file in the below path.
Magento2 root directory/app/Dckap/Newlink/Block/Header.php
Code:
<?php
namespace Dckap\Newlink\Block;
class Header extends \Magento\Framework\View\Element\Html\Link
{
protected $_template = 'Dckap_Newlink::link.phtml';
public function getHref()
{
return__( 'testuser');
}
public function getLabel()
{
return __('Test Link');
}
}
?>
Step 6:
Create template file that is referred to the block file in the below path.
Magento2 root directory/app/Dckap/Newlink/frontend/templates/link.phtml
Code:
<li>
<a <?php echo $block->getLinkAttributes() ?>><?php echo $block->escapeHtml($block->getLabel())?>
<?php echo($block->getCounter()) ? '<span>' . $block->escapeHtml($block->getCounter()) . '</span>' : ''; ?>
</a>
</li>
Step 7:
After completing the above steps run the below Magento2 upgrade command in the command prompt:
php bin/magento setup:upgrade
After running the above command, clear all the Magento cache and check whether the extension is installed successfully in the Magento 2 admin side.
Bookmarks