0

I am trying to develop a Magento 2 module following a number of tutorials and a lot of Google searching. I am also following a couple of Packt Publishing books and I am getting confused with an error I am getting when I try to render a simple page. I was able to create a very simple module and get a simple string to display when I simply echoed it inside an execute function in a controller. My problems started when I tried to move forward and develop a more realistic controller with a layout and template attached. Note I am using Magento 2.1.7 as the project this module is needed for is already developed and I am not able to upgrade.

In /app/code I have a directory called Steam with a module called GiftCardBalance

Inside that directory I have the following structure

GiftCardBalance/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Steam_GiftCardBalance',
__DIR__
);

GiftCardBalance/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="Steam_GiftCardBalance" setup_version="0.1.1">
    <sequence>
        <module name="ERP"/>
    </sequence>
</module>
</config>

GiftCardBalance/Controller/Index/Index.php

<?php

namespace Steam\GiftCardBalance\Controller\Index;

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

protected $resultPageFactory;

/**
 * Constructor
 *
 * @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);
}

/**
 * Execute view action
 *
 * @return \Magento\Framework\Controller\ResultInterface
 */
public function execute()
{
    return $this->resultPageFactory->create();
}
}

GiftCardBalance/etc/fronted/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="giftcardbalance" frontName="giftcardbalance">
        <module name="Steam_GiftCardBalance" />
    </route>
</router>
</config>

GiftCardBalance/view/frontend/layout/giftcardbalance_index_index.html

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="Steam\GiftCardBalance\Block\Index\Index" name="index.index" template="Steam_GiftCardBalance::index/index.phtml"/>
    </referenceContainer>
</body>
</page>

GiftCardBalance/templates/index/index.phtml

<h1>TEST TEST TEST</h1>

I have cleared the caches and have done a static deploy. I have also removed the contents of /pub/generation/Steam each time I make any changes

When I navigate to http://base_url/giftcardbalance/index/index

I get the Exception message "There has been an error processing your request" and when I look at the report specified I see a lengthy report but the start of the report is

a:4:{i:0;s:37:"Object DOMDocument should be created.";i:1;s:5425:"

In system.log I see

[2017-10-05 07:21:37] main.CRITICAL: Class Steam\GiftCardBalance\Block\Index\Index does not exist [] []
[2017-10-05 07:21:37] main.CRITICAL: Invalid block type: Steam\GiftCardBalance\Block\Index\Index [] []

This is where I am getting really confused. My Google search shows the DOM message popping up a lot and I have looked at a number of possibilities. The most common possibility seems to be file and directory permission issues but these all check out and I have set all file and directory permissions as suggested on a number of Magento sites.

As for the Class does not exist and the Invalid Block Type I am at a loss. The research I have done suggests I may have a capitalization error in a file or folder name and this would mess up the namespacing as this is a Linux server and case is important. If this is the issue I cannot see it.

The issue I am having is probably a simple issue and I am probably overlooking the obvious but after many hours of hunting I am at a loss and would appreciate some help and guidance

1

I can see your code, the error message clearly saying there is no block file.

Please create Block file.

<?php

namespace Steam\GiftCardBalance\Block\Index;

class Index extends \Magento\Framework\View\Element\Template{



}

and also change layout file extension .html to .xml

GiftCardBalance/view/frontend/layout/giftcardbalance_index_index.html

to

GiftCardBalance/view/frontend/layout/giftcardbalance_index_index.xml

And change the folder name

GiftCardBalance/etc/fronted/routes.xml

to

GiftCardBalance/etc/frontend/routes.xml

Layout file should be

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Steam\GiftCardBalance\Block\Index\Index" name="index.index" template="Steam_GiftCardBalance::index/index.phtml"/>
        </referenceContainer>
    </body>
</page>

After that try to re run the upgrade,clean and flush commands.

Feel free to ask if any queries.

0

giftcardbalance_index_index should be an xml file and not a html file. Also if you ever see DOMDocument error its mostly from corresponding layout xml file.

EDIT:

I now see that you have not created the block files as suggested by @magento two in Steam\GiftCardBalance\Block\Index\Index.php

<?php

namespace Steam\GiftCardBalance\Block\Index;

class Index extends \Magento\Framework\View\Element\Template{



}

Also template files should be located in GiftCardBalance/view/frontend/templates/index/index.phtml and not in GiftCardBalance/templates/index/index.phtml .

Flush cache and delete var/generation.

  • I have renamed giftcardbalance_index_index to be an xml file and cleared the caches, and again removed var.generation.Steam and I am still getting the same errors. – rickl Oct 5 '17 at 10:34
  • Please check updated answer – Vivek Kumar Oct 5 '17 at 12:06
  • Also don't forget to correct things suggested by @magento two – Vivek Kumar Oct 5 '17 at 12:11

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.