Question

I tried to follow steps according to this link: https://magecomp.com/blog/add-custom-tab-in-customer-account-section-magento-2/

The following code actually added a link to navigation bar but the page which should loads on click results 'not found' error

structure of my module

index.php :

<?php
namespace Magecomp\Mycard\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;

protected $session;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Model\Session $customerSession,
    PageFactory $resultPageFactory
    )
{
    $this->session = $customerSession;
    parent::__construct($context);
    $this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
    if (!$this->session->isLoggedIn())
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('customer/account/login');
        return $resultRedirect;
    }
    else
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('help'));
        return $resultPage;
    }
}

}

module.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magecomp_Mycard" setup_version="0.0.8">
    <sequence>
        <module name="Magento_Customer"/>
    </sequence>
</module>

customer_account.xml :

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="customer_account_navigation">
        <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-mycard" after="-">
            <arguments>
                <argument name="path" xsi:type="string">mycard/customer/index</argument>
                <argument name="label" xsi:type="string">Mycard</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

mycard_customer_index.xml :

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<body>
    <referenceBlock name="page.main.title">
        <action method="setPageTitle">
            <argument translate="true" name="title" xsi:type="string">My Card</argument>
        </action>
    </referenceBlock>
    <referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template" name="my_card" template="Magecomp_Mycard::mycard.phtml">
        </block>
    </referenceContainer>
</body>

mycard.phtml :

<?php
echo "Hello Friends";
?>

registration.php :

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magecomp_Mycard',
__DIR__
);

Could anybody help me please?

Was it helpful?

Solution

Create xml [VendorName/ModuleName/view/frontend/layout/customer_account.xml]


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-downloadable-products-link-custom">
                <arguments>
                    <argument name="path" xsi:type="string">downloadable/customer/products</argument>
                    <argument name="label" xsi:type="string">My Custom Link</argument>
                </arguments>
            </block>
        </referenceBlock>
   </body>
</page>

Here path is your custom path and label is your custom label.

More Detail

Clear cache.

[Update]

Missing part of your module is routes.xml. So create it.

Magecomp/Mycard/etc/frontend/routes.xml


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="mycard" frontName="mycard">
            <module name="Magecomp_Mycard" />
        </route>
    </router>
</config>

Clear cache.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top