Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top