{"id":59647,"date":"2016-09-16T16:14:59","date_gmt":"2016-09-16T16:14:59","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=59647"},"modified":"2026-02-11T13:05:32","modified_gmt":"2026-02-11T13:05:32","slug":"create-repositories-magento2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/","title":{"rendered":"Create Repositories in Magento 2"},"content":{"rendered":"\n<p>Today we\u2019ll explore how to create repositories in Magento 2 using best practices and clean architecture principles.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the Repository?<\/strong><\/h2>\n\n\n\n<p>Repositories are service contracts which are interface classes, and help to hide your business logic from the controller, model, and helper.<\/p>\n\n\n\n<p>To create your module&#8217;s repository, firstly you have to define it in the di.xml file at path:<\/p>\n\n\n\n<p>app\/code\/Webkul\/Hello\/etc\/di.xml<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:ObjectManager\/etc\/config.xsd&quot;&gt;\n    &lt;preference for=&quot;Webkul\\Hello\\Api\\TestRepositoryInterface&quot; type=&quot;Webkul\\Hello\\Model\\TestRepositoryModel&quot; \/&gt;\n&lt;\/config&gt;<\/pre>\n\n\n\n<p>In this file, we defined our repository file which is an interface class, and model file in which we define our methods declared&nbsp;in the repository class.<\/p>\n\n\n\n<p>Now, create the repository file at path:<\/p>\n\n\n\n<p>Webkul\\Hello\\Api\\TestRepositoryInterface.php<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\nnamespace Webkul\\Hello\\Api;\n\ninterface TestRepositoryInterface\n{\n    \/**\n     * Create or update a data\n     *\/\n    public function save(\\Webkul\\Hello\\Api\\Data\\TestInterface $test);\n\n    public function getById($testId);\n\n    \/**\n     * Delete test.\n     *\/\n    public function delete(\\Webkul\\Hello\\Api\\Data\\TestInterface $test);\n\n    \/**\n     * Delete test by ID.\n     *\/\n    public function deleteById($testId);\n}<\/pre>\n\n\n\n<p>Here, we declare&nbsp;our methods, like, getById, delete, deleteById, save.<\/p>\n\n\n\n<p>Now, create a model file in which we define our methods, at path:<\/p>\n\n\n\n<p>Webkul\\Hello\\Model\\TestRepositoryModel.php<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">namespace Webkul\\Hello\\Model;\n\nclass TestRepositoryModel implements \\Webkul\\Hello\\Api\\TestRepositoryInterface\n{\n\n    \/**\n     * Save test data.\n     *\/\n    public function save(\\Webkul\\Hello\\Api\\Data\\TestInterface $test)\n    {\n        \/\/your code\n    }\n    \/**\n     * Retrieve test data.\n     *\/\n    public function getById($testId)\n    {\n        \/\/your code\n    }\n\n    \/**\n     * Delete test.\n     *\/\n    public function delete(\\Webkul\\Hello\\Api\\Data\\TestInterface $test)\n    {\n        \/\/your code\n    }\n\n    \/**\n     * Delete test by test ID.\n     *\/\n    public function deleteById($testId)\n    {\n        \/\/your code\n    }\n}<\/pre>\n\n\n\n<p><br><br>Here, we define all the methods that we declared in our repository class.<\/p>\n\n\n\n<p>Here,&nbsp;\\Webkul\\Hello\\Api\\Data\\TestInterface in the data interface in which we define our data entities and their respective methods.<\/p>\n\n\n\n<p>Now, repositories are created for your module.<\/p>\n\n\n\n<p><strong>How to use repositories?<\/strong><\/p>\n\n\n\n<p>Here, is an example of a controller, in which we have a test ID, and by the use of repositories, we use the deleteById() function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">namespace Webkul\\Hello\\Controller\\test;\n\nuse Webkul\\Hello\\Api\\TestRepositoryInterface;\n\nclass Delete extends Action\n{\n\n    protected $_testReporitory;\n\n    public function __construct(\n        Context $context,\n        TestRepositoryInterface $testReporitory\n    ) {\n        $this-&gt;_testReporitory = $testReporitory;\n        parent::__construct(\n            $context\n        );\n    }\n\n    public function execute()\n    {\n        try {\n            $testId = 10;\/\/any id\n            $this-&gt;_testReporitory-&gt;deleteById($testId);\n        } catch (\\Exception $e) {\n            $this-&gt;messageManager-&gt;addException($e, $e-&gt;getMessage());\n        }\n    }\n}<\/pre>\n\n\n\n<p>I hope this blog will help you with creating repositories in Magento 2. You may also check our wide range of best <a data-wpel-link=\"internal\" href=\"https:\/\/store.webkul.com\/Magento-2.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Extensions<\/a>.<\/p>\n\n\n\n<p>Please reach out to our team via a&nbsp;<a href=\"https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/\" target=\"_blank\" rel=\"noreferrer noopener\" data-wpel-link=\"exclude\">support ticket<\/a>&nbsp;if you have any queries.<\/p>\n\n\n\n<p>Try this and if you have any queries then just comment below \ud83d\ude42<\/p>\n\n\n\n<p>Hope, it will help you.<\/p>\n\n\n\n<p>Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we\u2019ll explore how to create repositories in Magento 2 using best practices and clean architecture principles. What is the Repository? Repositories are service contracts which are interface classes, and help to hide your business logic from the controller, model, and helper. To create your module&#8217;s repository, firstly you have to define it in the <a href=\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":68,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302],"tags":[2070],"class_list":["post-59647","post","type-post","status-publish","format-standard","hentry","category-magento2","tag-magento2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create Repositories in Magento 2<\/title>\n<meta name=\"description\" content=\"we\u2019ll explore how to create repositories in Magento 2 using best practices and clean architecture principles.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create Repositories in Magento 2\" \/>\n<meta property=\"og:description\" content=\"we\u2019ll explore how to create repositories in Magento 2 using best practices and clean architecture principles.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-16T16:14:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-11T13:05:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bulbul\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bulbul\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/\"},\"author\":{\"name\":\"Bulbul\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/c9c6288b3950490ffdb37cb2a526996e\"},\"headline\":\"Create Repositories in Magento 2\",\"datePublished\":\"2016-09-16T16:14:59+00:00\",\"dateModified\":\"2026-02-11T13:05:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/\"},\"wordCount\":270,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"Magento2\"],\"articleSection\":[\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/\",\"url\":\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/\",\"name\":\"Create Repositories in Magento 2\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2016-09-16T16:14:59+00:00\",\"dateModified\":\"2026-02-11T13:05:32+00:00\",\"description\":\"we\u2019ll explore how to create repositories in Magento 2 using best practices and clean architecture principles.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/create-repositories-magento2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create Repositories in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/c9c6288b3950490ffdb37cb2a526996e\",\"name\":\"Bulbul\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/37ea7175f5ae6557d01bb38e147f6a02a540714ecdb71770d8ec554d4d34c23f?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/37ea7175f5ae6557d01bb38e147f6a02a540714ecdb71770d8ec554d4d34c23f?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Bulbul\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/bulbul896\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create Repositories in Magento 2","description":"we\u2019ll explore how to create repositories in Magento 2 using best practices and clean architecture principles.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/","og_locale":"en_US","og_type":"article","og_title":"Create Repositories in Magento 2","og_description":"we\u2019ll explore how to create repositories in Magento 2 using best practices and clean architecture principles.","og_url":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-09-16T16:14:59+00:00","article_modified_time":"2026-02-11T13:05:32+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Bulbul","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Bulbul","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/"},"author":{"name":"Bulbul","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/c9c6288b3950490ffdb37cb2a526996e"},"headline":"Create Repositories in Magento 2","datePublished":"2016-09-16T16:14:59+00:00","dateModified":"2026-02-11T13:05:32+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/"},"wordCount":270,"commentCount":3,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["Magento2"],"articleSection":["Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/create-repositories-magento2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/","url":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/","name":"Create Repositories in Magento 2","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2016-09-16T16:14:59+00:00","dateModified":"2026-02-11T13:05:32+00:00","description":"we\u2019ll explore how to create repositories in Magento 2 using best practices and clean architecture principles.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/create-repositories-magento2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/create-repositories-magento2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Create Repositories in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/c9c6288b3950490ffdb37cb2a526996e","name":"Bulbul","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/37ea7175f5ae6557d01bb38e147f6a02a540714ecdb71770d8ec554d4d34c23f?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/37ea7175f5ae6557d01bb38e147f6a02a540714ecdb71770d8ec554d4d34c23f?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Bulbul"},"url":"https:\/\/webkul.com\/blog\/author\/bulbul896\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/59647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=59647"}],"version-history":[{"count":8,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/59647\/revisions"}],"predecessor-version":[{"id":525982,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/59647\/revisions\/525982"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=59647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=59647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=59647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}