5

I want to add custom link in customer's navigation.I have developed below code but getting 404 - not found issue while click on the tab.

<?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="module">
            <arguments>
                <argument name="path" xsi:type="string">module/customer/list</argument>
                <argument name="label" xsi:type="string" translate="true">My Data</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>
</page>

Then I create a controller file name : List.php at modulename/controller/customer

<?php
    namespace namespace\modulename\Controller\Customer;
    use Magento\Sales\Controller\OrderInterface;
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;

    class List extends \Magento\Framework\App\Action\Action
    {
     /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Customer order history
     *
     * @return \Magento\Framework\View\Result\Page
     */
        public function execute()
        {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('My Data'));

        $block = $resultPage->getLayout()->getBlock('customer.account.link.back');
        if ($block) {
            $block->setRefererUrl($this->_redirect->getRefererUrl());
        }
        return $resultPage;
    }
    }

Block file at namespace/module/block/customer/test/list.php

<?php

namespace namespace\modulename\Controller\Customer;

use Magento\Sales\Controller\OrderInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class List extends \Magento\Framework\App\Action\Action
{
 /**
 * @var PageFactory
 */
protected $resultPageFactory;

/**
 * @param Context $context
 * @param PageFactory $resultPageFactory
 */
public function __construct(
    Context $context,
    PageFactory $resultPageFactory
) {
    $this->resultPageFactory = $resultPageFactory;
    parent::__construct($context);
}

/**
 * Customer order history
 *
 * @return \Magento\Framework\View\Result\Page
 */
public function execute()
{
    $resultPage = $this->resultPageFactory->create();
    $resultPage->getConfig()->getTitle()->set(__('My Orders'));

    $block = $resultPage->getLayout()->getBlock('customer.account.link.back');
    if ($block) {
        $block->setRefererUrl($this->_redirect->getRefererUrl());
    }
    return $resultPage;
}
}

and add xml file : modulename_test_list.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="modulename">
            <arguments>
                <argument name="path" xsi:type="string">modulename/customer/list</argument>
                <argument name="label" xsi:type="string" translate="true">My Data</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

2
  • were you able to resolve it? Aug 20, 2016 at 11:56
  • check if your frontname and route Id are same. Dec 11, 2016 at 15:25

2 Answers 2

0

The path is not modulename/customer/list , but route_id/customer/list , where route_id is what you have defined in routes.xml. Coincidentally they can be the same, but is not mandatory.

0
  1. Check your routes.xml with "frontName" same 'modulename'.
  2. If it is not working. Pls run command setup:di:compile

Your Answer

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

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