0

I wants to add a custom link in my account dashboard, and that link redirects to specified phtml file in magento2.

How to add specified controller to this.

Controller:

    <?php
namespace Vendor\Module\Controller\Customer;

class Index extends \Magento\Framework\App\Action\Action
{
   /**
  * Index Action*
  * @return void
  */
  public function execute()
  {
    $this->_view->loadLayout();
    $this->_view->renderLayout();
  }
}

test_customer_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">
  <head>
        <title>My Blog Post</title>
    </head>
    <update handle="customer_account"/>
    <body>
      <referenceBlock name="page.main.title">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">My Blog Post</argument>
            </action>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="module_customer" template="Vendor_Module::loy_rew.phtml" after="-" />
        </referenceContainer>
    </body>
</page>

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-nscore-link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">My Blog Post</argument>
                    <argument name="path" xsi:type="string">module/customer</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>
10
  • Please check my answer Commented Nov 5, 2018 at 5:02
  • Is it working or not ? @shivashankarm Commented Nov 5, 2018 at 6:59
  • Hi @Rohan sorry for the late reply Commented Nov 5, 2018 at 11:31
  • Its still redirecting to "Whoops, our bad... " page. Commented Nov 5, 2018 at 11:32
  • Can you please upload your xml, controller and phtml code? So, I can easily find your issue and solve it. Commented Nov 5, 2018 at 11:37

1 Answer 1

2

Create customer_account.xml file in /app/code/VendorName/ModuleName/view/frontend/layout/

<?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>
        <move element="customer-account-navigation-blog-link"  destination="customer_account_navigation" after="customer-account-navigation-wish-list-link"/>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-blog-link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">My Blog Post</argument>
                    <argument name="path" xsi:type="string">blog/customer</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Create controller Index.php at /app/code/VendorName/Blog/Controller/Customer/

<?php
namespace VendorName\Blog\Controller\Customer;

class Index extends \Magento\Framework\App\Action\Action
{
   /**
  * Index Action*
  * @return void
  */
  public function execute()
  {
    $this->_view->loadLayout();
    $this->_view->renderLayout();
  }
}

Create blog_customer_index.xml layout file at /app/code/VendorName/Blog/view/frontend/layout/

<?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">
  <head>
        <title>My Blog Post</title>
    </head>
    <update handle="customer_account"/>
    <body>
      <referenceBlock name="page.main.title">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">My Blog Post</argument>
            </action>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="blog_customer" template="VendorName_ModuleName::yourphtml.phtml" after="-" />
        </referenceContainer>
    </body>
</page>

Create yourphtml.phtml file at /app/code/VendorName/Blog/view/frontend/templates/

<h1> File call successfully </h1>

My Blog Post => Your tab lable

blog/customer => Your controller path

9
  • Thanks @Rohan , iam able to add a link by using above code ,but when I click on that link it's redirected to whoops bad page, how to add a specified phtml to that link Commented Nov 5, 2018 at 5:19
  • Did you create controller which you add in path ? Commented Nov 5, 2018 at 5:25
  • Yes , i have created a controller file Commented Nov 5, 2018 at 5:26
  • Can you please upload your controller and xml file code in question? Commented Nov 5, 2018 at 5:29
  • Check my updated answer. Commented Nov 5, 2018 at 5:40

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.