magento 2 custom module layout issue
-
11-10-2020 - |
Question
Created Magento 2 custom module but the layout seems not showing anything. It is supposed to show some output but it is showing white blank screen in the page.
Please help
My URL http://localhost/PG2/helloworld/Index/Index
Folder Structure: http://prntscr.com/g1t4sp
controller code
<?php
namespace Inchoo\Helloworld\Controller\Index;
use Magento\Framework\App\Action\Context;
class Index extends \Magento\Framework\App\Action\Action
{
protected $_resultPageFactory;
public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
{
$this->_resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
// echo "Hello ";
$resultPage = $this->_resultPageFactory->create();
return $resultPage;
}
}
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="helloworld" frontName="helloworld">
<module name="Inchoo_Helloworld" />
</route>
</router>
</config>
module.xml code
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Inchoo_Helloworld" setup_version="1.0.0">
</module>
</config>
helloworld_Index_Index
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
<body>
<referenceContainer name="content">
<block class="Inchoo\Helloworld\Block\Helloworld" name="helloworld" template="helloworld.phtml" />
</referenceContainer>
</body>
</page>
block class file
class Helloworld extends \Magento\Framework\View\Element\Template
{
public function getHelloWorldTxt()
{
return 'Hello world!';
}
}
Solution
I checked your code. You made simple file naming mistakes
1) Rename helloworld_Index_Index.xml
to helloworld_index_index.xml
(make router name in lowercase)
2) Remove space before file name at app/code/Inchoo/Helloworld/view/frontend/templates
rename '(space)helloworld.phtml' to 'helloworld.phtml'
Now keep this code in helloworld.phtml
to test your code
I am testing buddy
<?php echo $this->getHelloWorldTxt(); ?></h1>
Now remove var/generation and flush cache and try it's working now.
OTHER TIPS
The page should load with a 404 error if there is an issue with the code, not a white page. If you place a die('die');
into the controller's execute()
method and then your block's _prepareLayout()
method, you will see if they are being hit and there is a syntax error there. But your issue sounds like the localhost server is getting a 500 error, which points to your local system.
Looking over the code above, i don't see any issues (except that you use helloworld_Index_Index
and not helloworld_index_index
but in the dir path you show, the file is named correctly). Also, you should be able to hit the controller with just http://localhost/PG2/helloworld
, the index is redundant, but using it shouldn't create issues.
----UPDATE----
try changing your block class to this
class Helloworld extends \Magento\Backend\Block\Template
{
function _prepareLayout(){}
}