Today, we’re going to teach you guys how to implement a repository interface in your Magento 2 store.
A repository is basically a service contract that helps to hide the business logic from the controller, model, and helper.
In this post, we’ll show you exactly how to implement a repository interface in your Magento 2 store.
Step-By-Step Process to Implement a Repository Interface in Magento 2
Please follow the below steps to learn how to implement the repository interface in your Magento 2 store.
Step - 1
First of all, create a di.xml file in the app/code/MageDelight/Hello/etc/ folder and paste the following code:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="MageDelight\Hello\Api\TestRepositoryInterface" type="MageDelight\Hello\Model\TestRepositoryModel" /> </config>
With the above code, you’ll be creating the repository file, which is basically an interface class, and a model file where the methods are declared in the repository class.
Step - 2
After that, you need to create a TestRepositoryInterface.php file in the MageDelight\Hello\Api\ folder and paste the following code:
<?php
namespace MageDelight\Hello\Api;
interface TestRepositoryInterface
{
/**
* Create or update a data
*/
public function save(\MageDelight\Hello\Api\Data\TestInterface $test);
public function getById($testId);
/**
* Delete test.
*/
public function delete(\MageDelight\Hello\Api\Data\TestInterface $test);
/**
* Delete test by ID.
*/
public function deleteById($testId);
}
With the above code, you’ll be declaring the getById, deleteById, save, delete, etc. methods.
Step - 3
Next, create a TestRepositoryModel.php file in the MageDelight\Hello\Model\ folder and paste the following code:
namespace MageDelight\Hello\Model;
class TestRepositoryModel implements \MageDelight\Hello\Api\TestRepositoryInterface
{
/**
* Save test data.
*/
public function save(\MageDelight\Hello\Api\Data\TestInterface $test)
{
//your code
}
/**
* Retrieve test data.
*/
public function getById($testId)
{
//your code
}
/**
* Delete test.
*/
public function delete(\MageDelight\Hello\Api\Data\TestInterface $test)
{
//your code
}
/**
* Delete test by test ID.
*/
public function deleteById($testId)
{
//your code
}
}
The above code will simply define all methods in your repository class we declared earlier and the final repository will be created for your module.
Step - 4
Now, let’s say that you have a test id of a controller and you’d like to use deleteById() function through the repository.
Here’s how you can do it:
namespace MageDelight\Hello\Controller\test;
use MageDelight\Hello\Api\TestRepositoryInterface;
class Delete extends Action
{
protected $_testReporitory;
public function __construct(
Context $context,
TestRepositoryInterface $testReporitory
) {
$this->_testReporitory = $testReporitory;
parent::__construct(
$context
);
}
public function execute()
{
try {
$testId = 10;//any id
$this->_testReporitory->deleteById($testId);
} catch (\Exception $e) {
$this->messageManager->addException($e, $e->getMessage());
}
}
}
Conclusion
And that’s about it!
This is the most straightforward way to implement/create a repository interface in a Magento 2 store.
We hope that you found this tutorial helpful. However, if you still need our professional assistance with Magento 2 Development, be sure to reach out to us for help at any time.



