3

I tried to add new link in the customer account link sidebar with add code below in customer_account.xml with my custom module.

customer_account.xml :

<referenceBlock name="customer_account_navigation">
  <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-owner-reset-employee-password">
     <arguments>
        <argument name="path" xsi:type="string">reset/emppass</argument>
        <argument name="label" xsi:type="string" translate="true">Reset Employee Password</argument>
     </arguments>
  </block>
</referenceBlock>

It's all works and i can see new link there, but how can i add the page for it? So if customer click the link, the customer will send to the custom link page.

4
  • Is you question just about adding a custom page? In which page is above link is going to be shown?
    – P S
    Jan 8, 2018 at 3:57
  • Yes it is, it will be shown in customer account page, just like "My Orders" link in customer account sidebar, when customer clicked the link customer will send to My Orders page, and my custom page will show like that too. Jan 8, 2018 at 4:15
  • You need a routes.xml to create the route to your page and then you need a custom controller. Did you add that link through layout file inside your theme or inside a custom module? Jan 8, 2018 at 4:50
  • @ShawnAbramson I add the link inside my custome module Jan 8, 2018 at 4:58

1 Answer 1

2

For this you have to create a module. Inside your module create routes.xml in location app/code/Vendor/Module/etc/frontend/routes.xml

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

Create layout at location app/code/Vendor/Module/view/frontend/layout/reset_emppass_index.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>
        <referenceContainer name="content">
                 <container name="categories1"  htmlTag="div" htmlClass="categories">
                    <block class="Vendor\Module\Block\YourAnyBlock" name="myblock" template="Vendor_Module::index/index.phtml"/>
                </container>
        </referenceContainer>
    </body>
</page>

Next create a controller index and an action index at app/code/Vendor/Module/Controller/Emppass/Index.php

<?php


namespace Vendor\Module\Controller\Emppass;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

Finally create a template at app/code/Vendor/Module/view/frontend/templates/index.phtml

Hello this is my template. I can do anything inside this file.
9
  • It's 404 page not found, am i do something wrong? Jan 8, 2018 at 8:21
  • Yes you are doing something wrong coz, it's working in mine.
    – P S
    Jan 8, 2018 at 8:31
  • What page are you redirected. Share your steps in question so that I can check.
    – P S
    Jan 8, 2018 at 8:32
  • I create the link to testmagento.com/reset/emppass like in my question, and when i clicked the link, it's 404 error. Jan 8, 2018 at 8:50
  • 1
    Found it, the code template="Vendor_Module::index/index.phtml" should be template="index.phtml" in the layout, but my other question is, how can i made the custom page looklike in the customer account page? Jan 8, 2018 at 9:05

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.