1

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!';
            }
        }
  • Check my updated answer. I tested your code in my setup and found two little mistakes in your code :) – Prince Patel Jul 30 '17 at 11:58
1

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.

0

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(){}
}
  • By putting die('die'); in controller I can see the die without any issue. – Danial Butt Jul 29 '17 at 17:54
  • what is in your block class file? if it sees the controller, then the next file in the chain "should" be the layout xml. That is calling your block and setting your template. Try setting die statements in all of those and see where that fails, it will tell you which file has the error. – circlesix Jul 29 '17 at 19:23
  • oh, and what exact version of m2 are you running? – circlesix Jul 29 '17 at 19:24
  • This is what I can see prntscr.com/g2242v using url localhost/PG2/helloworld/Index/Index – Danial Butt Jul 30 '17 at 7:44
  • I am using 2.1.7 – Danial Butt Jul 30 '17 at 7:46

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.