So in this blog, I am going to explain how we can override the Header.php file.
Base Path: magento/vendor/magento/module-theme/Block/Html/Header.php
New Path: app/code/Bluethink/Overridemodule/Block/Html/Header.php
To override the Block file first we need to create a module core file that is registration.php and module.xml
Below are the steps :
step 1 : Create registration.php file
app/code/Bluethink/Overridemodule/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bluethink_Overridemodule', __DIR__ ); |
step 2 : Create module.xml file
app/code/Bluethink/Overridemodule/etc/module.xml
1 2 3 4 5 |
<?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="Bluethink_Overridemodule"> </module> </config> |
Step 3 : Create di.xml file
app/code/Bluethink/Overridemodule/etc/di.xml
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd"> <preference for="Magento\Theme\Block\Html\Header" type="Bluethink\Overridemodule\Block\Html\Header" /> </config> |
step 4 : Create Header.php file
app/code/Bluethink/Overridemodule/Block/Html/Header.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Bluethink\Overridemodule\Block\Html; /** * Html page header block * * @api * @since 100.0.2 */ class Header extends \Magento\Theme\Block\Html\Header { /** * Current template name * * @var string */ protected $_template = 'Magento_Theme::html/header.phtml'; /** * Retrieve welcome text * * @return string */ public function getWelcome() { if (empty($this->_data['welcome'])) { $this->_data['welcome'] = $this->_scopeConfig->getValue( 'design/header/welcome', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); } //return __($this->_data['welcome']); return "I am from Override Block Module!!!!!" ; } } |
After Run This Command
1 2 3 4 5 |
sudo php bin/magento setup:upgrade sudo php bin/magento setup:di:compile sudo php bin/magento setup:static-content:deploy -f sudo php bin/magento c:f sudo chmod -R 777 var/ pub/static generated/ |
Then Your Header Message is Changed
bluethinkinc_blog
2022-07-22