Below shows a list of common parent classes to extend in Magento 2 when creating your own functionality that extends the Magento core application.
If you need to add a frontend block class, the class should extend Magento\Framework\View\Element\Template
.
<?php
namespace [Vendor]\[Module]\Block;
class YourBlock extends \Magento\Framework\View\Element\Template
{
}
Adminhtml blocks can extend from Magento\Backend\Block\Template
, which in turn extend Magento\Framework\View\Element\Template
.
<?php
namespace [Vendor]\[Module]\Block\Adminhtml;
class YourBlock extends \Magento\Backend\Block\Template
{
}
To create a frontend controller class, ensure that you extend Magento\Framework\App\Action\Action
.
<?php
namespace [Vendor]\[Module]\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
}
Adminhtml controllers extend from \Magento\Backend\App\Action
, which include additional methods to check the user is authorised to view pages within the admin area.
<?php
namespace [Vendor]\[Module]\Controller\Adminhtml;
class YourController extends \Magento\Backend\App\Action
{
}
Helpers can be created by extending the Magento\Framework\App\Helper\AbstractHelper
class.
<?php
namespace [Vendor]\[Module]\Helper;
class YourHelper extends \Magento\Framework\App\Helper\AbstractHelper
{
}
Models should extend from Magento\Framework\Model\AbstractModel
.
<?php
namespace [Vendor]\[Module]\Model;
class YourModel extends \Magento\Framework\Model\AbstractModel
{
}
Similar to Magento 1, resource models and collections extend from different classes compared to a normal model.
Resource models should extend Magento\Framework\Model\ResourceModel\Db\AbstractDb
.
<?php
namespace [Vendor]\[Module]\Model\ResourceModel;
class YourResourceModel extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
}
Collections should extend Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
.
<?php
namespace [Vendor]\[Module]\Model\ResourceModel\[Entity];
class YourCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
}
Moving onto some implementation, observer class should implement the Magento\Framework\Event\ObserverInterface
interface.
<?php
namespace [Vendor]\[Module]\Observer;
class YourObserver implements \Magento\Framework\Event\ObserverInterface
{
}
To add a custom section, which belongs in your module’s CustomerData
directory, implement the Magento\Customer\CustomerData\SectionSourceInterface
interface.
<?php
namespace [Vendor]\[Module]\CustomerData;
class YourSection implements \Magento\Customer\CustomerData\SectionSourceInterface
{
}
When adding new database tables using the InstallSchema
, ensure that this class implements Magento\Framework\Setup\InstallSchemaInterface
.
<?php
namespace [Vendor]\[Module]\Setup;
class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
{
}
Similar with UpgradeSchema
classes, implement the Magento\Framework\Setup\UpgradeSchemaInterface
interface.
<?php
namespace [Vendor]\[Module]\Setup;
class UpgradeSchema implements \Magento\Framework\Setup\UpgradeSchemaInterface
{
}
When installing data, using the InstallData
class, ensure the class implements Magento\Framework\Setup\InstallDataInterface
.
<?php
namespace [Vendor]\[Module]\Setup;
class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
}
UpgradeData
classes should implement Magento\Framework\Setup\UpgradeDataInterface
.
<?php
namespace [Vendor]\[Module]\Setup;
class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
}
And lastly, Uninstall
classes should implement the Magento\Framework\Setup\UninstallInterface
interface.
<?php
namespace [Vendor]\[Module]\Setup;
class Uninstall implements \Magento\Framework\Setup\UninstallInterface
{
}
Note: This article is based on Magento Open Source version 2.1.9.