Magento 2 Override layout xml

Follow the below steps to override layout xml. Here I am creating a module ‘PHPCodez_Layout’ that will remove header and footer content from the active theme.

Declare Module

File : app/code/PHPCode/Layout/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="PHPCodez_Layout" setup_version="0.0.1"/>
</config>

Register the module

File : app/code/PHPCodez/Layout/registration.php

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

Create Layout XML file

File : app\code\PHPcodez\Layout\view\frontend\layout\default.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="header.container" remove="true" />
 <referenceContainer name="footer" remove="true" />
 </body>
</page>

Issue the following commands and you can see that header and footer contents are removed from the pages.

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
Share

Leave a Reply

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