Skip to content

Service

torreytsui edited this page Apr 5, 2016 · 8 revisions

Service Contract vs Web Service

Magento service layer is more like the service in Domain Driven Design.

Magento calls it service contract

Service layer

On the diagram, the service keyword appears twice: Web services and Module service contract. What we are referring to for FH integration is on the web services part, while the actual magento service layer is module service contract

The service layer is an abstraction for decoupling modules' dependencies. When there is any cross modules invocation needed, they can invoke the service layer. A service is a PHP interface. The interface guarantees not change and therefore this is the contract.

Service Contract as Web Service

Configure services as web APIs

Magento 2 seems to have taken care the Web Services part already.

Module service contract can be configured as Web Services.

Catalog product example

http://magento2-example.dev/rest/V1/products?searchCriteria[current_page]=1&searchCriteria[page_size]=1

<?xml version="1.0"?>
<response>
  <items>
    <item>
      <id>1</id>
      <sku>Oreo</sku>
      <name>Oreo</name>
      <attribute_set_id>4</attribute_set_id>
      <price>1</price>
      <status>1</status>
      <visibility>4</visibility>
      <type_id>simple</type_id>
      <created_at>2016-04-04 14:47:23</created_at>
      <updated_at>2016-04-04 14:47:23</updated_at>
      <product_links/>
      <options/>
      <tier_prices/>
      <custom_attributes>
        <item>
          <attribute_code>description</attribute_code>
          <value>&lt;p&gt;This is oreo&lt;/p&gt;</value>
        </item>
        <item>
          <attribute_code>meta_title</attribute_code>
          <value>Oreo</value>
        </item>
        <item>
          <attribute_code>meta_keyword</attribute_code>
          <value>Oreo</value>
        </item>
        <item>
          <attribute_code>meta_description</attribute_code>
          <value>Oreo &lt;p&gt;This is oreo&lt;/p&gt;</value>
        </item>
        <item>
          <attribute_code>image</attribute_code>
          <value>/o/r/oreo.png</value>
        </item>
        <item>
          <attribute_code>small_image</attribute_code>
          <value>/o/r/oreo.png</value>
        </item>
        <item>
          <attribute_code>thumbnail</attribute_code>
          <value>/o/r/oreo.png</value>
        </item>
        <item>
          <attribute_code>options_container</attribute_code>
          <value>container2</value>
        </item>
        <item>
          <attribute_code>required_options</attribute_code>
          <value>0</value>
        </item>
        <item>
          <attribute_code>has_options</attribute_code>
          <value>0</value>
        </item>
        <item>
          <attribute_code>url_key</attribute_code>
          <value>oreo</value>
        </item>
        <item>
          <attribute_code>swatch_image</attribute_code>
          <value>/o/r/oreo.png</value>
        </item>
        <item>
          <attribute_code>tax_class_id</attribute_code>
          <value>2</value>
        </item>
      </custom_attributes>
    </item>
  </items>
  <search_criteria>
    <filter_groups/>
    <page_size>1</page_size>
    <current_page>1</current_page>
  </search_criteria>
  <total_count>2047</total_count>
</response>

Magento/Catalog/etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">

    <!-- Product Service -->
    <route url="/V1/products" method="POST">
        <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="save"/>
        <resources>
            <resource ref="Magento_Catalog::products" />
        </resources>
    </route>
    <route url="/V1/products/:sku" method="PUT">
        <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="save" />
        <resources>
            <resource ref="Magento_Catalog::products" />
        </resources>
    </route>
    <route url="/V1/products/:sku" method="DELETE">
        <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="deleteById"/>
        <resources>
            <resource ref="Magento_Catalog::products" />
        </resources>
    </route>
    <route url="/V1/products" method="GET">
        <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="getList"/>
        <resources>
            <resource ref="Magento_Catalog::products" />
        </resources>
    </route>
    <route url="/V1/products/:sku" method="GET">
        <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="get"/>
        <resources>
            <resource ref="Magento_Catalog::products" />
        </resources>
    </route>
    ....
</routes>

Magento/Catalog/Api/ProductRepositoryInterface.php

interface ProductRepositoryInterface
{
    public function save(...);
    public function get(...);
    public function getById(...);
    public function delete(...);
    public function deleteById(...);
    public function getList(...);
}
Clone this wiki locally