Back to Top

load custom layout at frontend from controller in magento 2

Updated 26 March 2024

load custom layout at frontend from controller in magento 2

In this article we will learn how to load a custom layout from controller in Magento2.

So at first, we will create a layout file which is to be loaded from other controller under app/code/Namespace/Module/view/frontend/layout/ , let’s say module_custom_customlayout.xml Now write the following code in this file.

<?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="Namespace\Module\Block\Custom" name="custom_layout_load" template="Namespace_Module::custom.phtml" cacheable="false"/>
        </referenceContainer>
    </body>
</page>

There is also a page called on this layout, i.e. custom.phtml under app/code/Namespace/Module/view/frontend/templates/ . In this file I have only written the php code to print some string. You may change it according to your need.

Now the major part is to load this layout from a controller.
So create a controller , let’s say in my case the controller name is “Custom” under app/code/Namespace/Module/Controller/ and then create its action file, “Custom.php” under app/code/Namespace/Module/Controller/Custom/.
Here is the code snippet that will be written in file app/code/Namespace/Module/Controller/Custom/Custom.php to load layout of app/code/Namespace/Module/view/frontend/layout/module_custom_customlayout.xml file.

Searching for an experienced
Magento Company ?
Find out More
<?php
namespace Namespace\Module\Controller\Custom;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Custom extends Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $_resultPageFactory;

    /**
     * [__construct]
     * @param Context                          $context
     * @param PageFactory                      $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
    ) {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct(
            $context
        );
    }

    /**
     * loads custom layout
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
       $resultPage = $this->_resultPageFactory->create();
       $resultPage->addHandle('module_custom_customlayout'); //loads the layout of module_custom_customlayout.xml file with its name
       return $resultPage;
    }
}

Above code will load the file app/code/Namespace/Module/view/frontend/templates/custom.phtml  and then prints the string written in this file at frontend.

That’s all in this article, hope it will help you to load a layout file at frontend.Ttry the above code and if you have any issue just comment below. 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home