When you want to serve personalized experience to your shoppers by making changes to default layout, it always ends up at Magento Customization. Over the time Magento has topped Ecommerce CMS list with its functionality, features and power of extending the native functionalities. Compared to Magento 1, In Magento 2 overriding and manipulating with classes is becoming easier a developer to play quickly. At MageComp, we have never recommended modifying default core functionalities, but we can extend core functionalities using concepts of overriding which is a safe and easy way to achieve customization or development.
By default, there are three different ways to override core functionalities.
1. Using preference
2. Using event\observer
3. Using a plugin
While using overriding, sometimes you need to override the class, which reside in the framework. At that time, with the help of this tiny piece of code inside your custom extension, it will be easy for you.
For example, we need to override the Title.php of “vendor\magento\framword\View\Page“.
Then simply add this line to your di.xml file of your module/extension.
<pre class="lang:default decode:true">
<preference for="Magento\Framework\View\Page\Title" type="[VENDOR]\[EXTENSION]\View\Page\Title" />
Now, create the class [VENDOR]\[EXTENSION]\View\Page\Title that extends Magento\Framework\View\Page\Title and make your changes in that.
That’s it! You have successfully override framework class in Magento 2. You are free to play and manipulate this according to your need for overriding one or more class in Magento 2 Extension.
That’s it for today, Let us know if you are facing an issue while implementing using this code by commenting below.
Nice and simple explanation. Thanks