Magento 2 – Custom Module’s Frontend Controller Not Calling Layout

frontendlayoutmagento2

Below is URL to access

{site url}/magento2/module/mymodule/new

{folder path}\magento2\app\code\Custom\Module\Controller\MyModule\NewAction.php

namespace Custom\Module\Controller\MyModule;

class NewAction extends \Magento\Framework\App\Action\Action {

/**
 * @var \Magento\Framework\App\Cache\TypeListInterface
 */
protected $_cacheTypeList;

/**
 * @var \Magento\Framework\App\Cache\StateInterface
 */
protected $_cacheState;

/**
 * @var \Magento\Framework\App\Cache\Frontend\Pool
 */
protected $_cacheFrontendPool;

/**
 * @var \Magento\Framework\View\Result\PageFactory
 */
protected $resultPageFactory;

/**
 * @param Action\Context $context
 * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
 * @param \Magento\Framework\App\Cache\StateInterface $cacheState
 * @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
 * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
 */
public function __construct(
\Magento\Framework\App\Action\Context $context, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Framework\App\Cache\StateInterface $cacheState, \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool, \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
    parent::__construct($context);
    $this->_cacheTypeList = $cacheTypeList;
    $this->_cacheState = $cacheState;
    $this->_cacheFrontendPool = $cacheFrontendPool;
    $this->resultPageFactory = $resultPageFactory;
}

/**
 * Flush cache storage
 *
 */
public function execute() {

    $this->_view->loadLayout();
    $this->_view->getLayout()->initMessages();
    $this->_view->getPage()->getConfig()->getTitle()->set(__('Test Post'));
    /** @var \Magento\Framework\View\Result\Page $resultPage */
    $resultPage = $this->resultPageFactory->create();

    return $resultPage;
}

}

{folder path}\magento2\app\code\Custom\Module\view\frontend\layout\module_mymodule_new.xml

<?xml version="1.0"?>
<page 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="Magento\Framework\View\Element\Template" name="custom_module_post_form"
                   template="Custom_Module::index.phtml" />
        </referenceContainer>
    </body>
</page>

When u run URL on my WAMP it shows blank page. It's not calling layout file from controller.

Best Answer

Add layout column layout="1column"

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="custom_module_post_form"
                   template="Custom_Module::index.phtml" />
        </referenceContainer>
    </body>
</page>
Related Topic