1

I'm hoping for the community's help on a head scratcher.

I'm developing an extension on Magento 2.3.1, and have followed every single guide I've found / including using the magento 2.3 devdocs for inserting a link to the customer account dashboard & having my controller for the link return some test data. My extension is extremely simple and I can inject the link onto the customer account dashboard, but the url injected to my custom controller just 404s. I've tried multiple variations of the guides out there spanning 2.2 to 2.3.0, but none of them result in a non 404 when clicking the link.

I can see my custom link on the Customer Account Dashboard. I can click on the link, which has the correct path for my controller, as defined in customer_account.xml, but when I click on the link... I get a 404 page. Nothing in my system, errors, or debug log related to the 404 at all.

I've cleared cache, I've ensured my module is enabled, I've run setup:upgrade, setup:di:compile, and setup:static-content:deploy. I've ensured my magento2/var and magento2/generated folders (and subfolders) are owned by the proper m2 user.

I've tried naming my Block file something else. I've tried renaming my controller action from Index.php to DoThing.php (and making the expected change to my xml file name / and so-on), I've tried various combinations of a lot of changes that online guides/posts suggest would help, but nothing helps.

I must be missing something incredibly simple. Please see my module structure as defined below. Assume the Company/DoThing folder is in app/code/, eg:

/var/www/html/magento2/app/code/Company/DoThing/

My module is as such:

Base Extension Folder (in app/code/):

Company/DoThing:

  • registration.php:

    <?php
        \Magento\Framework\Component\ComponentRegistrar::register(
            \Magento\Framework\Component\ComponentRegistrar::MODULE,
            'Company_DoThing',
            __DIR__
        );
    
  • Block\Index.php

    class Index extends \Magento\Framework\View\Element\Template
    {
        public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = [])
        {
    
            parent::__construct($context, $data);
    
        }
    
        protected function _prepareLayout()
        {
            return parent::_prepareLayout();
        }
    }
    
  • Controller\Index\Index.php

    <?php 
    namespace Company\DoThing\Controller\Index;
    
    class Index extends \Magento\Framework\App\Action\Action
    {
        public function execute() {
            $this->_view->loadLayout();
            $this->_view->loadLayoutUpdates();
            $this->_view->getPage()->getConfig()->getTitle()->set(__('Do The Thing'));
            $this->_view->renderLayout();
        }
    }
    
  • etc\module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Company_DoThing" setup_version="1.0.0"></module>
    </config>
    
  • etc\frontend\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 id="dothing" frontName="dothing">
                <module name="Company_DoThing" />
            </route>
        </router>
    </config>
    
  • view\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-do-thing-link">
                <arguments>
                   <argument name="path" xsi:type="string">dothing/index</argument>
                   <argument name="label" translate="true" xsi:type="string">Do The Thing</argument>
                   <argument name="sortOrder" xsi:type="number">123</argument>
                </arguments>
             </block>
          </referenceBlock>
       </body>
    </page>
    

    For this one, I've tried

    <argument name="path" xsi:type="string">dothing/index/index</argument>
    

    but this also doesn't work.

  • view\frontend\layout\dothing_index_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">
        <update handle="customer_account"/>
        <body>
            <referenceContainer name="content">
                <block class="Company\DoThing\Block\Index" name="customer.account.do.thing" template="Company_DoThing::dothing.phtml" cacheable="false"/>
            </referenceContainer>
        </body>
    </page>
    
  • view\frontend\templates\dothing.phtml

    <?php
    ?>
    <div class="dothing-account">
        <span>testing testing 123</span>
    </div>
    

Thanks in advance for any help.

  • are you able to access controller directly ? – Pawan Jun 26 at 1:46
  • Hi @Pawan. If I go to the direct path of my controller, I still 404. I've tried: /dothing/index/index /dothing/index /dothing None of them result in anything but the 404 I've been getting. :/ – Art Jun 26 at 19:59
1

Try to change these files

view\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\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-outstocknotifications" ifconfig="outstocknotifications/general/enable">
                <arguments>
                    <argument name="path" xsi:type="string" >dothing</argument>
                    <argument name="label" xsi:type="string">Do The Thing</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
 </page>

Controller\Index\Index.php

<?php 
namespace Company\DoThing\Controller\Index;

use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action{

    public function __construct(
        Context $context
    ) 
    {
        parent::__construct($context);
    }


    public function execute()
    {  
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }

}

hope it works

  • does it works ? – ASQ Jun 26 at 6:20
  • Will check at work tomorrow and accept if it works- thanks abdus. – Art Jun 26 at 8:11
  • Hi @abdus. Neither of the changes to either file helped: I first added the constructor for the controller and tested that; same 404 issue. I then updated my customer_account.xml to simplify the "path", and changed the block class as per your suggestion. Still 404. These changes don't stop the 404 from occurring. Thanks for trying- I'm open to other suggestions if you have any. – Art Jun 26 at 19:58
0

So, my controller loads now, with the above-posted code, but it required me to turn off production mode.

We were testing in our integration environment in production mode, and recompiling our module via:

bin/magento module:enable Company_DoThing bin/magento setup:upgrade bin/magento setup:di:compile bin/magento setup:static-content:deploy

But this must not have been fully generating code for my module... very odd. Will try to figure out why this controller 404's in production mode but works in developer mode.

Thanks to everyone who tried helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.