You can create a skeleton of module from Module Generator.
After creating a module structure update your module to database using command line code :
1 |
php bin/magento setup:upgrade |
To every magento project there needs certain level of customization on overriding magento core classes. Editing and adding our custom code on magento core classes is a bad practice since they all will be overwritten during magento upgrade. So, there comes a concept of magento classes override. For this magento 2 has two approaches, they are :
1. Using preference
2. Using plugins
Today I will be discussing on using preference for overriding core customer module.
Step #1 – Create a di.xml file in a folder Sagar/ClassOverride/etc
1 2 3 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Customer\Block\Form\Register" type="Sagar\ClassOverride\Block\Form\Register" /> </config> |
Step #2 – The next step to override Magento2 Block is to create a Register.php Block file in the folder Sagar/ClassOverride/Block/Form
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace Sagar\ClassOverride\Block\Form; class Register extends \Magento\Customer\Block\Form\Register { public function __construct(){ die('block overriden'); } } ?> |
Step #1 – Create a di.xml file in a folder Sagar/ClassOverride/etc
1 2 3 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Customer\Model\Customer" type="Sagar\ClassOverride\Model\Customer" /> </config> |
Step #2 – The next step to override Magento2 Model is to create a Customer.php Model file in the folder Sagar/ClassOverride/Model
1 2 3 4 5 6 7 8 9 |
<?php namespace Sagar\ClassOverride\Model; class Customer extends \Magento\Customer\Model\Customer { public function __construct(){ die('model override'); } } ?> |
Step #1 – Create a di.xml file in a folder Sagar/ClassOverride/etc
1 2 3 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Customer\Controller\Account\CreatePost" type="Sagar\ClassOverride\Controller\Account\CreatePost" /> </config> |
Step #2 – The next step to override Magento2 Controller is to create a CreatePost.php Controller file in the folder Sagar/ClassOverride/Controller/Account
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace Sagar\ClassOverride\Controller\Account; class CreatePost extends \Magento\Customer\Controller\Account\CreatePost { public function __construct(){ die('controller'); } } ?> |
Step #1 – Create a di.xml file in a folder Sagar/ClassOverride/etc
1 2 3 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Customer\Controller\Account\CreatePost" type="Sagar\ClassOverride\Controller\Account\CreatePost" /> </config> |
Step #2 – The next step to override Magento2 Helper is to create a Address.php Helper file in the folder Sagar/ClassOverride/Helper
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace Sagar\ClassOverride\Helper; class Address extends \Magento\Customer\Helper\Address { function __construct() { die('helper overridden'); } } ?> |
You can override classes of other modules using similar approach.
You can find complete code at : Github
Note : The preferences in di.xml are commented. Please uncomment them to use to your own.
More blogs on overriding classes in magento 2 are Mukesh Chapagain Blog & Inchoo