0

I am attempting to create a block with a template in my own module. When I come to view the website (front end) I get an HTTP 500 error (admin side still loads). I have checked in the var/log folder for any debug or system messages but none are reported. Disabling the module allows the website to load again.

Below are the steps, along with file directories and file content for what I have done.

My Magento 2 main directory is StoreTest

Registering a module (I successfully did this stage but I will include it for clarity)

Created module.xml and registration.php in the following folders.

StoreTest/Code/Mymodules/Mybanner/etc/module.xml StoreTest/Code/Mymodules/Mybanner/registration.php

Module.xml content

    <?xml version="1.0"?>
    <!--
    /**
     *
     * @category    mymodules
     * @package     mymodules_mybanner
     * @copyright   Copyright (c) 2012 mymodules
     * @license     none
     */
    -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="mymodules_mybanner" setup_version="1.0.0"/>
    </config>

Registration.php content

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

Registered the module (by logging into the server using PuTTy and navigating to the StoreTest/bin/ directory)

php magento module:enable mymodules_mybanner

Then I ran

php magento setup:upgrade

Create a Block, layout and template file.

To create a block I added a MyBlock.php file to the following location StoreTest/app/code/Mymodules/Mybanner/Block/

MyBlock.php content

<?php

namespace Mymodules\Mybanner\Block;

class MyBlock extends \Magento\Framework\View\Element\Template
{
    /**
     * Return the HTML
     * @return string
     */
    public function getHtmlString()
    {
        return "Testing";
    }
}

I created a layout file called cms_index_index.xml in the following location StoreTest/app/code/Mymodules/Mybanner/view/frontend/layout/

cms_index_index.xml content

<?xml version="1.0"?>
<!--
/**
 * @category    Mymodules
 * @package     mymodules_mybanner
 * @copyright   Copyright (c) 2012 Magestore (http://www.magestore.com/)
 * @license     http://www.magestore.com/license-agreement.html
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="main">
           <block class="Mymodules\Mybanner\Block\MyBlock" name="mybanner.example" template="banner.phtml" >
                <action method="getHtmlString" />
           </block>
        </referenceContainer>
    </body>
</page>

Lastly I created a banner.phtml file in the following location StoreTest/app/code/Mymodules/Mybanner/view/frontend/templates/

Banner.phtml content

<?php 
/*
  * @var \Mymodules\Mybanner\Block\MyBlock $block 
*/ 
?>
<h3>Howdy folks!</h3>
<p><?php echo $block->getHtmlString(); ?></p>

My folder structure looks like the following

StoreTest
|-- Mymodules
    |-- Mybanner
        |-- Block
        |    |--MyBlock.php
        |-- etc
        |    |--module.xml
        |-- view
        |    |-- frontend
        |         |-- layout
        |              |--cms_index_index.xml
        |         |-- templates
        |              |-- banner.phtml 
        |-- registration.php
0

Two things to check:

Namespace

Module.xml & registration.php you have:

<module name="mymodules_mybanner"

and you enabled module:

php magento module:enable mymodules_mybanner

but in MyBlock.php you are calling the Module block via:

namespace Mymodules\Mybanner\Block;

and in cms_index_index.xml you are rendering block:

<block class="Mymodules\Mybanner\Block\MyBlock"

Notice the capitalization discrepancies within the namespace. Please correct these.

Deploy your static content

After correcting the namespace and enabling the module, run setup:upgrade again. Then run:

php magento setup:static-content:deploy
  • Thank you for the information. I have done the requested changes. Unfortunately I am still getting the same problem. – Scott Oct 2 '16 at 15:53
  • Hmmm i would double check to ensure your files/folders are all named appropriately. – Joshua34 Oct 2 '16 at 15:54
  • I delete the module and started again with the advice you gave me. It works. Thank you. – Scott Oct 3 '16 at 10:47
  • Glad to hear Scott. Please accept my answer, marking your question answered. Thanks. – Joshua34 Oct 3 '16 at 14:08

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.