0

I'm new to Magento. In my Magento 2.3.3 project, when adding a new link to my account navigation and I want to show the content in the right-side container block. Unfortunately, when I press on it, it goes to a new window that says the page doesn't exist. My code below:

In app/code/PCE/NewPassword/web/frontend/layout/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\Customer\Block\Account\SortLinkInterface" name="customer-account-navigation-newpassword-change-link">
                <arguments>
                    <argument name="path" xsi:type="string">newpassword/change</argument>
                    <argument name="label" xsi:type="string">New Password</argument>
                    <argument name="sortOrder" xsi:type="number">250</argument>
                </arguments>
            </block>
         
        </referenceBlock>
    </body>
</page>

In app/code/PCE/NewPassword/web/frontend/layout/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="newpassword" id="newpassword">
         <module name="PCE_NewPassword"/>
     </route>
  </router>
</config>

In app/code/PCE/NewPassword/web/frontend/layout/newpassword_change_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="PCE\NewPassword\Block\ResetPassword" name="newpassword.change.index" template="PCE_NewPassword::change/index.phtml" cacheable="false" />
        </referenceContainer>
    </body>
</page>

In app/code/PCE/NewPassword/Controller/Change/Index.php

<?php
namespace PCE\NewPassword\Controller\Change;
 
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();
    }
}
?>

In app/code/PCE/NewPassword/Block/ResetPassword.php

<?php
namespace PCE\NewPassword\Block;
class ResetPassword 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';
    }
    
}
?>

In app/code/PCE/NewPassword/web/frontend/index.phtml

<h2>
    <?php echo $block->getHelloWorld(); ?>
</h2>
<?php 
echo 'My First Link Page';
?>

Where I make the mistake and how can I resolve the issue? Thanks in advance.

2
  • Is your module folder NewPassowrd or NewPassword? The first is spelt wrong and doesn't match the namespace so it'll break.
    – Mark Rees
    Jan 25, 2021 at 17:20
  • Sorry, the i write that wrong in the question. The folder is named NewPassword like it's write in the code. I edit the question.
    – andei95
    Jan 26, 2021 at 7:01

2 Answers 2

0

Your file structure is wrong but it's not your misspelling in the question.

routes.xml files (for the frontend) go into etc/frontend so yours would be app/code/PCE/NewPassword/etc/frontend/routes.xml

Layouts go into the view/<area>/layout folder so in your case app/code/PCE/NewPassword/view/frontend/layout/newpassword_change_index.xml and app/code/PCE/NewPassword/view/frontend/layout/customer_account.xml

Templates go into the view/<area>/templates folder so yours should be app/code/PCE/NewPassword/view/frontend/templates/change/index.phtml

The template declaration in the block element of a layout tells Magento where to look for the template inside the templates directory. So template="PCE_NewPassword::change/index.phtml" tells Magento to go to the app/code/PCE/NewPassword/view/frontend/templates folder and then look for index.phtml in the change folder.

Your 404 is a symptom of the incorrect file structure caused by Magento not being able to see that your routes.xml file exists where it expects it to be.

1
  • That was the problem. For everyone who want to add a new link to customer navigation this is how you can. But with modification from Mark(routes must be in etc/frontend and index.phtml must be in view/frontend/templates/change/index.phtml)
    – andei95
    Jan 26, 2021 at 7:36
0

I can see a typo in your Module Directory Name. Rename your module directory to NewPassword to resolve the 404.

Your module is PCE_NewPassword

So the directory should be NewPassword not NewPassowrd

Wrong

app/code/PCE/NewPassowrd/Controller/Change/Index.php

Right

app/code/PCE/NewPassword/Controller/Change/Index.php

Wrong

app/code/PCE/NewPassowrd/web/frontend/layout/customer_account.xml

Right

app/code/PCE/NewPassword/view/frontend/layout/customer_account.xml

3
  • Sorry, I write wrong in the question. The module is called NewPassword, so it's not from there. I edit the question.
    – andei95
    Jan 26, 2021 at 7:02
  • @andei95 as the other answer points out, the directory structure are wrong
    – Haijerome
    Jan 26, 2021 at 7:36
  • yes, now i moved routes.xml in etc/frontend and index.phtml in view/frontend/templates/change and now works.
    – andei95
    Jan 26, 2021 at 7:43

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.