Magento2 Admin Module – Sample Custom Admin Module Showing Blank Page

magento2

I am trying to display something when click on a link from admin menu. So i created a simple module and when i click from the admin menu it redirect to mydomain/secure-manage/helloworld/index/index/key/… with status 200 but the page is blank.

In devtool i can see the css and js are loaded but nothing inside the body tag.

1.folder structure

Inchoo
 Helloworld

  -Block
    -Adminhtml
      Helloworld.php

  -Controller
    -Adminhtml
      -Index
        Index.php

  -etc
    -module.xml
    -adminhtml
      -menu.xml
      -routes.xml

  -view
     adminhtml
      layout
       -helloworld_index_index.xml
      templates
       -helloworld.phtml

  registration.php

2.Block/Adminhtml/Helloworld.php

<?php
 namespace Inchoo\Helloworld\Block;
 class Helloworld extends \Magento\Framework\View\Element\Template
 {
   public function getHelloWorldTxt()
   {
    return 'Hello world!';
   }
 }

3.Controller/Adminhtml/Index/Index.php

<?php

 namespace Inchoo\Helloworld\Controller\Adminhtml\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()
   {
     $resultPage = $this->_resultPageFactory->create();
     // print_r($resultPage);exit;
     return $resultPage;
    }
 }

4.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="Inchoo_Helloworld" setup_version="1.0.0">
  </module>
</config>

5.etc/adminhtml/menu.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc
/menu.xsd">
  <menu>
    <add id="Inchoo_Helloworld::first_pincollect_pincheck"
         title="Greetings"
         module="Inchoo_Helloworld"
         sortOrder="50"
         dependsOnModule="Inchoo_Helloworld"
         resource="Magento_Backend::content" />

    <add id="Inchoo_Helloworld::second_pincollect_pincheck"
         title="Manage Pincodes"
         module="Inchoo_Helloworld"
         sortOrder="0"
         action="helloworld/index"
         parent="Inchoo_Helloworld::first_pincollect_pincheck"
         resource="Magento_Backend::content" />
  </menu>
</config>

6.etc/adminhtml/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="admin">
    <route id="helloworld" frontName="helloworld">
        <module name="Inchoo_Helloworld" />
    </route>
  </router>
</config>

7.view/adminhtml/layout/helloworld_index_index.xml

<?xml version="1.0"?>
<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\Adminhtml\Helloworld" 
        name="helloworld" template="helloworld.phtml" />
    </referenceContainer>
  </body>
</page>

8.view/adminhtml/templates/helloworld.phtml

<p>Hello World!</p>

how can i debug this, please help me guys.

Thanks in advance.

Best Answer

I am giving you the exact code and path please copy and paste the below file.

app/code/Inchoo/Helloworld/registration.php

<?php

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

app/code/Inchoo/Helloworld/Block/Adminhtml/Index/Index.php

<?php

    namespace Inchoo\Helloworld\Block\Adminhtml\Index;

    class Index extends \Magento\Backend\Block\Widget\Container
    {



        public function __construct(\Magento\Backend\Block\Widget\Context $context,array $data = [])
        {
            parent::__construct($context, $data);
        }



    }

app/code/Inchoo/Helloworld/Controller/Adminhtml/Index/Index.php

<?php

    namespace Inchoo\Helloworld\Controller\Adminhtml\Index;


    class Index extends \Magento\Backend\App\Action
    {
        public function execute()
        {
            $this->_view->loadLayout();
            $this->_view->getLayout()->initMessages();
            $this->_view->renderLayout();
        }
    }

    ?>

app/code/Inchoo/Helloworld/etc/module.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
        <module name="Inchoo_Helloworld" setup_version="1.0.0"></module>
            <sequence>
                <module name="Magento_Backend"/>
                 <module name="Magento_Sales"/>
                <module name="Magento_Quote"/>
                <module name="Magento_Checkout"/>
            </sequence>
    </config>

app/code/Inchoo/Helloworld/etc/adminhtml/routes.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
        <router id="admin">
            <route id="helloworld" frontName="helloworld">
                <module name="Inchoo_Helloworld" before="Magento_Adminhtml" />
            </route>
        </router>
    </config>

app/code/Inchoo/Helloworld/etc/adminhtml/menu.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/menu.xsd">
        <menu>
            <add id="Inchoo_Helloworld::parent" title="Helloworld" module="Inchoo_Helloworld" sortOrder="100" resource="Inchoo_Helloworld::parent"/>
            <add id="Inchoo_Helloworld::index" title="Helloworld Index" module="Inchoo_Helloworld" sortOrder="10" action="helloworld/index" resource="Inchoo_Helloworld::index" parent="Inchoo_Helloworld::parent"/>

        </menu>
    </config>

app/code/Inchoo/Helloworld/view/adminhtml/layout/helloworld_index_index.xml

<?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
        <head>
            <title>Hello World</title>
        </head> 
        <body>
            <referenceContainer name="content">
                <block class="Inchoo\Helloworld\Block\Adminhtml\Index\Index" name="helloworld_block_adminhtml_index_index"  template="Inchoo_Helloworld::helloworld_index_index.phtml" />
            </referenceContainer>
        </body>
    </page>

app/code/Inchoo/Helloworld/view/adminhtml/templates/helloworld_index_index.phtml

<?php echo "Hello World template"; ?>

N:B After put all the files to the specific path don't forget to run this command from CLI -

php bin/magento setup:upgrade

php bin/magento cache:flush
Related Topic