Question

I'm adding a new page for my company, and that requires the use of a module. After some searching, I got the routes to function. However, the page doesn't seem to load the block/template as defined in my module_controller_action.xml file.

Module structure for as far as relevant:

-+ MyCompany
 -+ CustomRouter
  -+ Controller
   -+ Test
     - Test.php (Gets called as expected)
   - Router.php
  -- etc/...
  -+ view
   -+ frontend
    -+ layout
      - customrouter_test_test.xml
    -+ templates
      - configure_price_list.phtml

I'm using a block from Magento/Customer/Blocks/Account/Dashboard/Info. This block already has all that I need. I tried using an exact copy, but didn't notice any change.

My files: Test.php:

<?php
namespace MyCompany\CustomRouter\Controller\Test;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Test extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;

    /**
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    { // This function gets called 100% when expected, so the problem isn't in the router
        $this->_view->loadLayout();
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('Configure Price List'));
        return $resultPage;
    }
}

customrouter_test_test.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" layout="1column">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Customer\Block\Account\Dashboard\Info" name="configure_price_list" template="configure_price_list.phtml" cacheable="false" />
        </referenceContainer>
    </body>
</page>

configure_price_list.phtml:

<?php
// Made by (my name)
 ?>

 <div class=configure-price-list>
    <p>Template Loaded</p>
 </div>

The page loads, and the page title gets displayed as I set it in Test.php. However, none of the content displays, as if customrouter_test_test.xml does not exist. Is there anyone who can point me to the missing link here ?

(Did try to watch a number of tutorials, according to them, current setup should work.)

EDIT: After request, here's routes.xml:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="customrouter" frontName="customrouter">
            <module name="MyCompany_CustomRouter" />
        </route>
    </router>
</config>

And at the risk of stating the obvious, I'm loading [domain]/customrouter/test/test. I'm also loading another url I redirected with a custom router, which looks exactly the same (just the page heading I set in Test.php).

EDIT 2: I might have found a solution. But I'm still working on what caused the problem. currently im file-for-file switching a working custom action to act exactly like mine.

Was it helpful?

Solution

"Answer" might not be truly valid here.

It turns out the problem was with the block I imported from outside. The block I imported was located at Magento\Customer\Block\Account\Dashboard\Info. I first copied it to my own module, with the exact same contents, and it still didn't work. I then stripped out half of the functionality (just deleted function that I thought I wasn't going to need), and it started working.

The block constructor's arguments is reduced to:

\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,

from:

\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Magento\Customer\Helper\View $helperView,

And of course, I deleted everything those other arguments were required for. And it worked.

I still don't understand how or why, but this might help other people with similar problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top