Magento 2 Layout Issues – How to Fix Layout Not Loading

layoutmagento2

I put custom module in app/code/. Layout is not loading. On my custom route is blank white page. Debug showing that handles =null. This is code
/app/code/Namespace/ModuleName/view/frontend/layout/test_index_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="namespace\Modulename\Block\ModuleBlock" name="form" template="namespace_ModuleName::form.phtml" />
    </referenceContainer>
       </body>

Namespace/Modulename/Controller/Index/Index.php

 namespace Namespace\ModuleName\Controller\Index;

class Index extends \Namespace\ModuleName\Controller\Index
{
    /**
     *
     *
     */
    public function execute()
    {

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

**/app/code/Namespace/ModuleName/Controller/Index.php

<?php
namespace Namespace\ModuleName\Controller;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\App\RequestInterface;
use Magento\Store\Model\ScopeInterface;

/**
 * Contact index controller
 */
abstract class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * Recipient email config path
     */
    const XML_PATH_EMAIL_RECIPIENT = 'trans_email/ident_general/email';

    /**
     * Recipient email config path
     */
    const XML_PATH_EMAIL_RECIPIENT_2 = 'contact/email/recipient_email';

    /**
     * Sender email config path
     */
    const XML_PATH_EMAIL_SENDER = 'contact/email/sender_email_identity';

    /**
     * Email template config path
     */
    const XML_EMAIL_TEMPLATE = 'custom_cats_email_template';


    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;

    /**
     * @var \Magento\Framework\Translate\Inline\StateInterface
     */
    protected $inlineTranslation;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
     * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        parent::__construct($context);
        $this->_transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
    }

    /**
     * Dispatch request
     *
     * @param RequestInterface $request
     * @return \Magento\Framework\App\ResponseInterface
     * @throws \Magento\Framework\Exception\NotFoundException
     */
    public function dispatch(RequestInterface $request)
    {
        return parent::dispatch($request);
    }
}

/app/code/Namespace/ModuleName/view/frontend/templates/form.phtml

<h1>Hello</h1>

route.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="test" frontName="test" >
        <module name="Namespace_ModuleName" />
    </route>
</router>

Best Answer

Your module have some error, please follow below steps to have it work,

Step1 : app/code/Test/Module/Block/Form.php

<?php 

namespace Test\Module\Block;

use Magento\Framework\View\Element\Template;

class Form  extends Template
{

}
?>

Step2: app/code/Test/Module/Controller/Index/Index.php

<?php 
namespace Test\Module\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {


         $this->_view->loadLayout();

        $this->_view->renderLayout();

    }
}
?>

Step3: app/code/Test/Module/etc/frontend/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="module" frontName="module">
            <module name="Test_Module" />
        </route>
    </router>
</config>

Step4: app/code/Test/Module/etc/module.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015  All rights reserved.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Test_Module" schema_version="1.0.0" setup_version="1.0.0"/> 
</config>

Step4 : app/code/Test/Module/view/frontend/layout/module_index_index.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="Test\Module\Block\Form" name="form" template="Test_Module::form.phtml" />
    </referenceContainer>
       </body>
</page>

Step5: app/code/Test/Module/view/frontend/templates/form.phtml

<h1>Hello</h1>

Step6: app/code/Test/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Test_Module',
    __DIR__
);
?>

After running php bin/magento setup:upgrade it will show output@url localhost/magentoce27/index.php/module/index/index

Related Topic