Magento – Layout file is not loading for custom page magento2

layoutmagento2

I have created custom pages a lot of times, but never face such issue. I have tried 'echo' in controller action, it is working. But all this time the layout file is not loading for that page.

Here are my code files.

Controller/Customer/Test.php

class Test extends \Magento\Framework\App\Action\Action
{
   protected $_pageFactory;

   public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $pageFactory)
   {
       $this->_pageFactory = $pageFactory;
       return parent::__construct($context);
   }

  public function execute()
   {

       $page =  $this->_pageFactory->create();
       return $page;
   }
}

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="orderbysku" frontName="orderbysku">
            <module name="Vendor_Module" />
        </route>
    </router>
</config>

view/frontend/layout/orderbysku_customer_test.xml

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

Any kind of help is appreciated, i'm really having a hard time to figure it out.

Best Answer

1. Create registration.php in app/code/Vendor/Module

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

2. Create module.xml in app/code/Vendor/Module/etc

<?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="Vendor_Module" setup_version="2.0.1" />    
</config>

3. Create routes.xml in app/code/Vendor/Module/etc/frontend

<?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="orderbysku" frontName="orderbysku">
            <module name="Vendor_Module" />
        </route>
    </router>
</config>

4.Create Test.php in app/code/Vendor/Module/Controller/Customer

<?php

namespace Vendor\Module\Controller\Customer;

class Test extends \Magento\Framework\App\Action\Action
{
   protected $_pageFactory;

   public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $pageFactory)
   {
       $this->_pageFactory = $pageFactory;
       return parent::__construct($context);
   }

  public function execute()
   {

       $page =  $this->_pageFactory->create();
       return $page;
   }
}

5. Create orderbysku_customer_test.xml in app/code/Vendor/Module/view/frontend/layout

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

6. Create index.phtml in app/code/Vendor/Module/view/frontend/templates

This is test template
Related Topic