4

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?

3
  • What you mean tab, you want to add custom link in left nav in frontend?
    – Sohel Rana
    Commented Sep 16, 2017 at 8:08
  • Yes like "Addresses" and "My Orders", I'm going to have another custom link, what shall I do?
    – M Taheri
    Commented Sep 16, 2017 at 8:29
  • You need to add this controller mycard/customer/index
    – Magento 2
    Commented Sep 16, 2017 at 11:33

1 Answer 1

11

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.

13
  • Thanks for your reply. I posted my complete module code, but I can not open the link! please read the code and tell me if I did something wrong.
    – M Taheri
    Commented Sep 16, 2017 at 9:28
  • Check updated answer.
    – Sohel Rana
    Commented Sep 16, 2017 at 10:46
  • I added routes.xml file in etc/frontend/ but again that error 404 happened! :(
    – M Taheri
    Commented Sep 16, 2017 at 11:31
  • 1
    Download from dropbox.com/s/9j15na4783lb8nf/Magecomp.zip?dl=0
    – Sohel Rana
    Commented Sep 17, 2017 at 6:55
  • 1
    Magecomp forget to add route.xml file
    – HaFiz Umer
    Commented Jun 22, 2019 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.