2

I have fairly simple REST API request to get guest country code by his ip. In my block file this works perfectly fine when using curl:

    $url = "http://........./".$guestIp."?access_key=".$access_key.$jsonOutput;
    $this->_curl->get($url);
    $response = $this->_json->unserialize($this->_curl->getBody());
    $countryCode = $response['country_code'];

    return $countryCode;

Now when should I create Api/Data interfaces, repository interfaces in this case if it is needed?

  • the purpose of a API and Data repository is to hide the storage related logic. A client of a repository should not care whether the returned entity is held in memory in an array, is retrieved from a MySQL database, fetched from a remote API or from a file. – Aaditya Oct 10 '18 at 7:25
4

In general, the purpose of a repository is to hide the storage related logic.

  • A client of a repository should not care whether the returned entity is held in memory in an array, is retrieved from a MySQL database, fetched from a remote API or from a file.
  • If a third party module use only the repositories, Magento can change how and where data is stored, and the module will continue to work, despite these deep changes.
  • Repositories generally have methods like findById(), findByName(), put() or remove().
  • In Magento these commonly are called getbyId(), save() and delete(), not even pretending they are doing anything else but wrap CRUD DB operations.

Magento 2 repository methods can easily be exposed as API resources, making them valuable for integrations with third party systems or headless Magento instances.


  • With service contracts usage (Api/Data interfaces), it will be possible to expose custom module functionality as web API just by adding config etc/webapi.xml
  • Improved capabilities for integration with 3rd party systems based on service contracts will be added in the future, as well as queue support.
  • If there is another module dependent on your module, it can just rely on service interfaces. This reduces coupling between modules and minimizes risk of accidental breaking of dependent module.

enter image description here

Now when should I create Api/Data interfaces, repository interfaces in this case if it is needed?

  • Service contracts enhance the modularity of Magento. They enable Magento and third-party developers to report system dependencies through composer.json files and, consequently, guarantee compatibility among Magento versions. This compatibility ensures that merchants can easily upgrade Magento.
  • Data entities are a side benefit of service contracts. The database tables that normally support these entities can be complicated. For example, some attributes might be stored in an EAV table, so a set of MySQL database tables might define a single data entity . Data entities in a service contract reveal a simpler data model than the data model in an underlying relational database schema . Eventually, you will be able to use different storage technologies for different data collections. For example, you could use a NoSQL database to replace product tables.
  • Using the @api tag - Backward compatibility can be indicated by the use of @api. For more information, see Backward compatibility.

Magento 2 Api/Data, When To Create It?

As always, the answer is “It depends”. (In Vinai Kopp's words)

  1. If your entities will be used by other modules, then yes, you probably want to add a repository.
  2. There is another factor that should be added into the equation: in Magento 2, repositories can easily be exposed as Web API - that is REST and SOAP - resources.
  3. If that is interesting to you because of third party system integrations or a headless Magento setup, then again, yes, you probably want to add a repository for your entity.

For more Information

  1. https://devdocs.magento.com/guides/v2.0/extension-dev-guide/service-contracts/service-contracts.html
  2. https://devdocs.magento.com/guides/v2.0/extension-dev-guide/service-contracts/service-to-web-service.html
  3. http://vinaikopp.com/2017/02/18/magento2_repositories_interfaces_and_webapi/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.