
Magento 2.x – How to add a customer myaccount link and its corresponding page
So, in some cases we will need to add a link in the customer my account frontend section. To do so, we should perform the following basic steps:
- The first and easiest part, is adding the link, to do so, create the following layout file app/code/Vendor/Module/view/frontend/layout/default.xml
1234567891011121314151617181920<?xml version="1.0"?><!--/*** Copyright © 2016 Magento. All rights reserved.* See COPYING.txt for license details.*/--><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\Customer\Block\Account\SortLinkInterface" name="test-link"><arguments><argument name="path" xsi:type="string">mymodule/customthing/index</argument><argument name="label" xsi:type="string">This is my link</argument><argument name="sortOrder" xsi:type="number">150</argument></arguments></block></referenceBlock></body></page> - Then, we will need this custom controller, to process the request once the link is clicked, so we will need a couple of things:
- app/code/Vendor/Module/etc/frontend/routes.xml
12345678<?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 id="mymodule" frontName="mymodule"><module name="Vendor_Module" /></route></router></config> - app/code/Vendor/Module/Controller/Customthing/Index.php
1234567891011121314151617181920212223242526272829<?phpnamespace Vendor\Module\Controller\Customthing;use Magento\Framework\App\Action\Context;use Magento\Framework\View\Result\PageFactory;class Index extends \Magento\Framework\App\Action\Action{protected $_resultPageFactory;public function __construct(Context $context,PageFactory $resultPageFactory){$this->_resultPageFactory = $resultPageFactory;parent::__construct($context);}public function execute(){$resultPage = $this->_resultPageFactory->create();$resultPage->addHandle('mymodule_customthing_index'); // Loads the new layout file$this->_view->getPage()->getConfig()->getTitle()->set(__('This is my section title'));return $resultPage;}} - app/code/Vendor/Module/view/frontend/layout/mymodule_customthing_index.xml
123456789101112131415<?xml version="1.0"?><!--/*** Copyright © Magento, Inc. All rights reserved.* See COPYING.txt for license details.*/--><page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"><update handle="customer_account"/><body><referenceContainer name="content"><block class="Magento\Framework\View\Element\Template" name="customer_account.customthing" template="Vendor_Module::customer/custom_thing.phtml" cacheable="false"/></referenceContainer></body></page> - app/code/Vendor/Module/view/frontend/templates/customer/custom_thing.phtml
1Hi there, this is the content i want to show!
- app/code/Vendor/Module/etc/frontend/routes.xml
- Afterwards, you will need to perform those steps on your CLI:
123rm -rf generated/*php bin/magento cache:cleanphp bin/magento setup:di:compile - Now make a try, log in the frontend with a customer, and you will see something like this on your myaccount section
Happy coding!