Magento – Magento 2 Module development Class does not exist error

classmagento-2.1.7moduletemplate

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

Best Answer

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.

Related Topic