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