I am trying to add two link to my account "link 1" "link 2". links that will load just a simple "testing" message. I got the link to display but if i click the link i get an oops error page.
The following is an example: How to add a new page to the new link in the Customer Account page
How do i do about fixing this in detail.
1. Create customer_account layout XML file
app/code/YourNamespace/YourModule/view/frontend/layout/customer_account.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customer_account_navigation">
<!-- Add menu to the end of the sidebar -->
<block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-your-first-link">
<arguments>
<argument name="path" xsi:type="string">yourmodule/yourfirstlink/index</argument>
<argument name="label" xsi:type="string">Your First Link</argument>
</arguments>
</block>
<block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-your-second-link">
<arguments>
<argument name="path" xsi:type="string">yourmodule/yoursecondlink/index</argument>
<argument name="label" xsi:type="string">Your Second Link</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
2. Create your module’s layout XML file
app/code/YourNamespace/YourModule/view/frontend/layout/yourmodule_yourfirstlink_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<head>
<title>
Your First Link
</title>
</head>
<body>
<referenceContainer name="content">
<block class="YourNamespace\YourModule\Block\YourBlock" name="yourmodule.firstlink.index" template="YourNamespace_YourModule::yourfirstlink/index.phtml" cacheable="false" />
</referenceContainer>
</body>
</page>
app/code/YourNamespace/YourModule/view/frontend/layout/yourmodule_yoursecondlink_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<head>
<title>
Your Second Link
</title>
</head>
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="yourmodule.secondlink.index" template="YourNamespace_YourModule::yoursecondlink/index.phtml" cacheable="false" />
</referenceContainer>
</body>
</page>
3. Create Controller class files
app/code/YourNamespace/YourModule/Controller/YourFirstLink/Index.php
<?php
namespace YourNamespace\YourModule\Controller\YourFirstLink;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @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);
}
/**
* Default customer account page
*
* @return void
*/
public function execute()
{
return $this->resultPageFactory->create();
}
}
?>
app/code/YourNamespace/YourModule/Controller/YourSecondLink/Index.php
<?php
namespace YourNamespace\YourModule\Controller\YourSecondLink;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @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);
}
/**
* Default customer account page
*
* @return void
*/
public function execute()
{
return $this->resultPageFactory->create();
}
}
?>
4. Create Block class file
app/code/YourNamespace/YourModule/Block/YourBlock.php
<?php
namespace YourNamespace\YourModule\Block;
class YourBlock extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Backend\Block\Template\Context $context,
array $data = []
)
{
parent::__construct($context, $data);
}
public function getHelloWorld()
{
return 'Hello World';
}
}
?>
5. Create Template files
app/code/YourNamespace/YourModule/view/frontend/templates/firstlink/index.phtml
<h1> first page </h1>
app/code/YourNamespace/YourModule/view/frontend/templates/secondlink/index.phtml
<h1> second Page </h1>
6. Create registration.php
app/code/YourNamespace/YourModule/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'YourNamespace_YourModule',
__DIR__
);
6/30/2020 Updates ( Changed YourNamespace/YourModule to MyAccount_Links
app/code/YourNamespace/YourModule/view/frontend/layout/customer_account.xml
Changed Path to "yourroute/yourfirstlink/index"
<?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">
<!-- Add menu to the end of the sidebar -->
<block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-your-first-link">
<arguments>
<argument name="path" xsi:type="string">yourroute/yourfirstlink/index</argument>
<argument name="label" xsi:type="string">Link 1</argument>
</arguments>
</block>
<block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-your-second-link" after="customer-account-navigation-your-first-link" >
<arguments>
<argument name="path" xsi:type="string">yourroute/yoursecondlink/index</argument>
<argument name="label" xsi:type="string">link 2</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
app/code/YourNamespace/YourModule/view/frontend/layout/yourroute_yourfirstlink_index.xml
Changed file name to lowercase "yourroute_yourfirstlink_index.xml"
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<head>
<title>
Your First Link
</title>
</head>
<body>
<referenceContainer name="content">
<block class="MyAccount\Links\Block\yourblock" name="yourroute/yourfirstlink/index" template="MyAccount_Links::yourfirstlink/index.phtml" cacheable="false" />
</referenceContainer>
</body>
</page>
app/code/YourNamespace/YourModule/view/frontend/layout/yourroute_yoursecondlink_index.xml
Changed file name yoursecondlink to lowercase and changed file="yourroute/yoursecondlink/index"
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<head>
<title>
Your Second Link
</title>
</head>
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="yourroute/yoursecondlink/index" template="MyAccount_Links::yoursecondlink/index.phtml" cacheable="false" />
</referenceContainer>
</body>
</page>
app/code/YourNamespace/YourModule/Controller/yourfirstlink/Index.php
Changed folder "yourfirstlink" to lowercase
<?php
namespace MyAccount\Links\Controller\yourfirstlink;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @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);
}
/**
* Default customer account page
*
* @return void
*/
public function execute()
{
return $this->resultPageFactory->create();
}
}
?>
app/code/YourNamespace/YourModule/Controller/yoursecondlink/Index.php
Changed folder to lowercase "yoursecondlink"
<?php
namespace MyAccount\Links\Controller\yoursecondlink;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @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);
}
/**
* Default customer account page
*
* @return void
*/
public function execute()
{
return $this->resultPageFactory->create();
}
}
?>
app/code/YourNamespace/YourModule/Block/yourblock.php
Change file name to lovercase "yourblock.php"
<?php
namespace MyAccount\Links\Block;
class yourblock extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Backend\Block\Template\Context $context,
array $data = []
)
{
parent::__construct($context, $data);
}
public function getHelloWorld()
{
return 'Hello World';
}
}
?>
app/code/YourNamespace/YourModule/etc/module.xml
Added a Module
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="MyAccount_Links" setup_version="0.0.1">
<sequence>
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
app/code/YourNamespace/YourModule/etc/frontend/routes.xml
Added 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="yourroute" id="yourmodule">
<module name="MyAccount_Links"/>
</route>
</router>
</config>