Table of Contents for
Magento 2 - Build World-Class online stores

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Magento 2 - Build World-Class online stores by Jonathan Bownds Published by Packt Publishing, 2017
  1. Cover
  2. Table of Contents
  3. Magento 2 - Build World-Class online stores
  4. Magento 2 - Build World-Class online stores
  5. Credits
  6. Preface
  7. 1. Module 1
  8. 1. Magento Fundamentals
  9. XAMPP installation
  10. Magento
  11. Summary
  12. 2. Magento 2.0 Features
  13. An introduction to the Magento order management system
  14. Magento 2.0 command-line configuration
  15. The command-line utility
  16. Summary
  17. 3. Working with Search Engine Optimization
  18. Store configuration
  19. SEO and searching
  20. SEO catalog configuration
  21. Google Analytics tracking code
  22. Optimizing Magento pages
  23. Summary
  24. 4. Magento 2.0 Theme Development – the Developers' Holy Grail
  25. Magento 2.0 theme structure
  26. The Magento Luma theme
  27. Magento theme inheritance
  28. CMS blocks and pages
  29. Custom variables
  30. Creating a basic Magento 2.0 theme
  31. Summary
  32. 5. Creating a Responsive Magento 2.0 Theme
  33. Composer – the PHP dependency manager
  34. Building the CompStore theme
  35. CSS preprocessing with LESS
  36. Applying new CSS to the CompStore theme
  37. Creating the CompStore logo
  38. Applying the theme
  39. Creating CompStore content
  40. Customizing Magento 2.0 templates
  41. Summary
  42. 6. Write Magento 2.0 Extensions – a Great Place to Go
  43. Using the Zend framework
  44. Magento 2.0 extension structure
  45. Developing your first Magento extension
  46. The Twitter REST API
  47. The TweetsAbout module structure
  48. Using TwitterOAuth to authenticate our extension
  49. Developing the module
  50. Summary
  51. 7. Go Mobile with Magento 2.0!
  52. Adjusting the CompStore theme for mobile devices
  53. The Magento 2.0 responsive design
  54. The Magento UI
  55. Implementing a new CSS mixin media query
  56. Adjusting tweets about extensions for mobile devices
  57. Summary
  58. 8. Speeding up Your Magento 2.0
  59. Indexing and caching Magento
  60. Indexing and re-indexing data
  61. The Magento cron job
  62. Caching
  63. Fine-tuning the Magento hosting server
  64. Selecting the right Magento hosting service
  65. Apache web server deflation
  66. Enabling the expires header
  67. Minifying scripts
  68. Summary
  69. 9. Improving Your Magento Skills
  70. Magento knowledge center
  71. Improving your Magento skills
  72. Summary
  73. 2. Module 2
  74. 1. Magento 2 System Tools
  75. Installing Magento 2 sample data via GUI
  76. Installing Magento 2 sample data via the command line
  77. Managing Magento 2 indexes via the command line
  78. Managing Magento 2 cache via the command line
  79. Managing Magento 2 backup via the command line
  80. Managing Magento 2 set mode (MAGE_MODE)
  81. Transferring your Magento 1 database to Magento 2
  82. 2. Enabling Performance in Magento 2
  83. Configuring Redis for backend cache
  84. Configuring Memcached for session caching
  85. Configuring Varnish as the Full Page Cache
  86. Configuring Magento 2 with CloudFlare
  87. Configuring optimized images in Magento 2
  88. Configuring Magento 2 with HTTP/2
  89. Configuring Magento 2 performance testing
  90. 3. Creating Catalogs and Categories
  91. Create a Root Catalog
  92. Create subcategories
  93. Manage attribute sets
  94. Create products
  95. Manage products in a catalog grid
  96. 4. Managing Your Store
  97. Creating shipping and tax rules
  98. Managing customer groups
  99. Configuring inventories
  100. Configuring currency rates
  101. Managing advanced pricing
  102. 5. Creating Magento 2 Extensions – the Basics
  103. Initializing extension basics
  104. Working with database models
  105. Creating tables using setup scripts
  106. Creating a web route and controller to display data
  107. Creating system configuration fields
  108. Creating a backend data grid
  109. Creating a backend form to add/edit data
  110. 6. Creating Magento 2 Extensions – Advanced
  111. Using dependency injection to pass classes to your own class
  112. Modifying functions with the use of plugins – Interception
  113. Creating your own XML module configuration file
  114. Creating your own product type
  115. Working with service layers/contracts
  116. Creating a Magento CLI command option
  117. 3. Module 3
  118. 1. Planning for Magento
  119. Technical considerations
  120. Global-Website-Store methodology
  121. Planning for multiple stores
  122. Summary
  123. 2. Managing Products
  124. Managing products the customer focused way
  125. Creating products
  126. Managing inventory
  127. Pricing tools
  128. Autosettings
  129. Related products, up-sells, and cross-sells
  130. Importing products
  131. Summary
  132. 3. Designs and Themes
  133. The concept of theme inheritance
  134. Default installation of design packages and themes
  135. Installing third-party themes
  136. Inline translations
  137. Working with theme variants
  138. Customizing themes
  139. Customizing layouts
  140. Summary
  141. 4. Configuring to Sell
  142. Payment methods
  143. Shipping methods
  144. Managing taxes
  145. Transactional e-mails
  146. Summary
  147. 5. Managing Non-Product Content
  148. Summary
  149. 6. Marketing Tools
  150. Promotions
  151. Newsletters
  152. Using sitemaps
  153. Optimizing for search engines
  154. Summary
  155. 7. Extending Magento
  156. The new Magento module architecture
  157. Extending Magento functionality with Magento plugins
  158. Building your own extensions
  159. Summary
  160. 8. Optimizing Magento
  161. Indexing and caching
  162. Caching in Magento 2 – not just FPC
  163. Tuning your server for speed
  164. Summary
  165. 9. Advanced Techniques
  166. Version control
  167. Magento cron
  168. Backing up your database
  169. Upgrading Magento
  170. Summary
  171. 10. Pre-Launch Checklist
  172. System configurations
  173. Design configurations
  174. Search engine optimization
  175. Sales configurations
  176. Product configurations
  177. Maintenance configurations
  178. Summary
  179. Index

Working with service layers/contracts

A service layer/contract is a fixed interface to get and store data without knowing the underlying layer. It is possible to swap the way the data is stored without changing the service layer.

A service layer consists of three interface types:

  • Data interface: A data interface is a read-only presentation of a record, and therefore, this type of interface only has getters to represent a data record.
  • Repository interface: A repository interface gives access to read and write (and delete) data. Every repository interface has the following methods:
    • getList: This returns a list of records based on the (optionally) provided search parameters
    • get: This loads the data from the database and returns a data interface for the specified ID
    • save: This saves the record specified in the data interface
    • delete: This deletes the record specified in the data interface
    • deleteById: This deletes the record specified by the ID
  • Management interface: In a management interface, it is possible to specify special management functions that are not related to the repository.

Using a service layer also makes it easy to extend your module to access the web API; you only need to add a declaration in the appropriate XML to configure the link from the API command to the right interface.

How to do it…

In this recipe, we will add the option to read, create, or delete a record through a service layer contract:

  1. Create a repository interface where the available commands are declared:

    Api/DemoRepositoryInterface.php

    <?php
    namespace Genmato\Sample\Api;
    
    interface DemoRepositoryInterface
    {
      /**
      * Save demo list item.
      *
      * @api
      * @param \Genmato\Sample\Api\Data\DemoInterface $demo
      * @return \Magento\Customer\Api\Data\GroupInterface
      * @throws \Magento\Framework\Exception\InputException If there is a problem with the input
      * @throws \Magento\Framework\Exception\NoSuchEntityException If a group ID is sent but the group does not exist
      * @throws \Magento\Framework\Exception\State\InvalidTransitionException
      *If saving customer group with customer group code that is used by an existing customer group
      * @throws \Magento\Framework\Exception\LocalizedException
      */
      public function save(\Genmato\Sample\Api\Data\DemoInterface $demo);
    
      /**
      * Get demo list item by ID.
      *
      * @api
      * @param int $id
      * @return \Genmato\Sample\Api\Data\DemoInterface
      * @throws \Magento\Framework\Exception\NoSuchEntityException If $groupId is not found
      * @throws \Magento\Framework\Exception\LocalizedException
      */
      public function getById($id);
    
      /**
      * Retrieve demo list items.
      *
      * The list of demo items can be filtered
      *
      * @api
      * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
      * @return \Genmato\Sample\Api\Data\DemoSearchResultsInterface
      * @throws \Magento\Framework\Exception\LocalizedException
      */
      public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
    
      /**
      * Delete demo list item.
      *
      * @api
      * @param \Genmato\Sample\Api\Data\DemoInterface $demo
      * @return bool true on success
      * @throws \Magento\Framework\Exception\StateException If customer group cannot be deleted
      * @throws \Magento\Framework\Exception\LocalizedException
      */
      public function delete(\Genmato\Sample\Api\Data\DemoInterface $demo);
    
      /**
      * Delete demolist by ID.
      *
      * @api
      * @param int $id
      * @return bool true on success
      * @throws \Magento\Framework\Exception\NoSuchEntityException
      * @throws \Magento\Framework\Exception\StateException If customer group cannot be deleted
      * @throws \Magento\Framework\Exception\LocalizedException
      */
      public function deleteById($id);
    }
  2. Create the repository resource model. Here, the defined functions from the interface have the actual code:

    Model/ResourceModel/DemoRepository.php

    <?php
    namespace Genmato\Sample\Model\ResourceModel;
    
    use Genmato\Sample\Model\DemoRegistry;
    use Genmato\Sample\Model\DemoFactory;
    use Genmato\Sample\Api\Data\DemoInterface;
    use Genmato\Sample\Api\Data\DemoInterfaceFactory;
    use Genmato\Sample\Api\Data\DemoExtensionInterface;
    use Genmato\Sample\Api\Data\DemoSearchResultsInterfaceFactory;
    use Genmato\Sample\Model\Demo;
    use Genmato\Sample\Model\ResourceModel\Demo as DemoResource;
    use Genmato\Sample\Model\ResourceModel\Demo\Collection;
    use Genmato\Sample\Api\DemoRepositoryInterface;
    
    use Magento\Framework\Exception\CouldNotDeleteException;
    use Magento\Framework\Exception\CouldNotSaveException;
    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Framework\Api\SearchCriteriaInterface;
    use Magento\Framework\Reflection\DataObjectProcessor;
    use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
    
    class DemoRepository implements DemoRepositoryInterface
    {
    
      /** @var DemoRegistry  */
      private $demoRegistry;
    
      /** @var DemoFactory  */
      private $demoFactory;
    
      /** @var DemoInterfaceFactory  */
      private $demoDataFactory;
    
      /** @var  Demo */
      private $demoResourceModel;
    
      /**
      * @var DataObjectProcessor
      */
      private $dataObjectProcessor;
    
      /**
      * @var DemoSearchResultsInterfaceFactory
      */
      protected $searchResultsFactory;
    
      /**
      * @var JoinProcessorInterface
      */
      protected $extensionAttributesJoinProcessor;
    
      /**
      * @param DemoRegistry $demoRegistry
      * @param DemoFactory $demoFactory
      * @param DemoInterfaceFactory $demoDataFactory
      * @param DemoResource $demoResourceModel
      * @param DataObjectProcessor $dataObjectProcessor
      * @param DemoSearchResultsInterfaceFactory $searchResultsFactory
      * @param JoinProcessorInterface $extensionAttributesJoinProcessor
      */
      public function __construct(
        DemoRegistry $demoRegistry,
        DemoFactory $demoFactory,
        DemoInterfaceFactory $demoDataFactory,
        DemoResource $demoResourceModel,
        DataObjectProcessor $dataObjectProcessor,
        DemoSearchResultsInterfaceFactory $searchResultsFactory,
        JoinProcessorInterface $extensionAttributesJoinProcessor
      ) {
        $this->demoRegistry = $demoRegistry;
        $this->demoFactory = $demoFactory;
        $this->demoDataFactory = $demoDataFactory;
        $this->demoResourceModel = $demoResourceModel;
        $this->dataObjectProcessor = $dataObjectProcessor;
        $this->searchResultsFactory = $searchResultsFactory;
        $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
      }
      /**
      * {@inheritdoc}
      */
      public function save(DemoInterface $demo)
      {
        /** @var Demo $demoModel */
        $demoModel = $this->demoFactory->create();
    
        if ($demo->getId()) {
          $demoModel->load($demo->getId());
        }
        $demoModel
        ->setTitle($demo->getTitle())
        ->setIsVisible($demo->getIsVisible())
        ->setIsActive($demo->getIsActive());
    
        try {
          $demoModel->save();
        } catch (\Exception $exception) {
          throw new CouldNotSaveException(__($exception->getMessage()));
        }
        return $demoModel->getData();
      }
    
      /**
      * {@inheritdoc}
      */
      public function getById($id)
      {
        $demoModel = $this->demoRegistry->retrieve($id);
        $demoDataObject = $this->demoDataFactory->create()
          ->setId($demoModel->getId())
          ->setTitle($demoModel->getTitle())
          ->setCreationTime($demoModel->getCreationTime())
          ->setUpdateTime($demoModel->getUpdateTime())
          ->setIsVisible($demoModel->getIsVisible())
          ->setIsActive($demoModel->getIsActive());
        return $demoDataObject;
      }
    
      /**
      * {@inheritdoc}
      */
      public function getList(SearchCriteriaInterface $searchCriteria)
      {
        $searchResults = $this->searchResultsFactory->create();
        $searchResults->setSearchCriteria($searchCriteria);
    
        /** @var Collection $collection */
        $collection = $this->demoFactory->create()->getCollection();
        foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
          foreach ($filterGroup->getFilters() as $filter) {
            $condition = $filter->getConditionType() ?: 'eq';
            $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
          }
        }
        $searchResults->setTotalCount($collection->getSize());
        $sortOrders = $searchCriteria->getSortOrders();
        if ($sortOrders) {
          /** @var SortOrder $sortOrder */
          foreach ($sortOrders as $sortOrder) {
            $collection->addOrder(
              $sortOrder->getField(),
              ($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
            );
          }
        }
        $collection->setCurPage($searchCriteria->getCurrentPage());
        $collection->setPageSize($searchCriteria->getPageSize());
        /** @var DemoInterface[] $demos */
        $demos = [];
        /** @var Demo $demo */
        foreach ($collection as $demo) {
          /** @var DemoInterface $demoDataObject */
          $demoDataObject = $this->demoDataFactory->create()
            ->setId($demo->getId())
            ->setTitle($demo->getTitle())
            ->setCreationTime($demo->getCreationTime())
            ->setUpdateTime($demo->getUpdateTime())
            ->setIsVisible($demo->getIsVisible())
            ->setIsActive($demo->getIsActive());
    
          $demos[] = $demoDataObject;
        }
        $searchResults->setTotalCount($collection->getSize());
        return $searchResults->setItems($demos);
      }
    
      /**
      * Delete demo list item.
      *
      * @param DemoInterface $demo
      * @return bool true on success
      * @throws \Magento\Framework\Exception\StateException If customer group cannot be deleted
      * @throws \Magento\Framework\Exception\LocalizedException
      */
      public function delete(DemoInterface $demo)
      {
        return $this->deleteById($demo->getId());
      }
    
      /**
      * Delete demo list item by ID.
      *
      * @param int $id
      * @return bool true on success
      * @throws \Magento\Framework\Exception\NoSuchEntityException
      * @throws \Magento\Framework\Exception\StateException If customer group cannot be deleted
      * @throws \Magento\Framework\Exception\LocalizedException
      */
      public function deleteById($id)
      {
        $demoModel = $this->demoRegistry->retrieve($id);
    
        if ($id <= 0) {
          throw new \Magento\Framework\Exception\StateException(__('Cannot delete demo item.'));
        }
    
        $demoModel->delete();
        $this->demoRegistry->remove($id);
        return true;
      }
    
    
    }
  3. Create the registry class; this stores the loaded records:

    Model/DemoRegistry.php

    <?php
    /**
    * Sample
    *
    * @package Genmato_Sample
    * @author  Vladimir Kerkhoff <support@genmato.com>
    * @created 2015-12-23
    * @copyright Copyright (c) 2015 Genmato BV, https://genmato.com.
    */
    namespace Genmato\Sample\Model;
    
    use Genmato\Sample\Api\Data\DemoInterface;
    use Magento\Framework\Exception\NoSuchEntityException;
    
    class DemoRegistry
    {
      /**
      * @var array
      */
      protected $registry = [];
    
      /**
      * @var DemoFactory
      */
      protected $demoFactory;
    
      /**
      * @param DemoFactory $demoFactory
      */
      public function __construct(DemoFactory $demoFactory)
      {
        $this->demoFactory = $demoFactory;
      }
    
      /**
      * Get instance of the Demo Model identified by an id
      *
      * @param int $demoId
      * @return Demo
      * @throws NoSuchEntityException
      */
      public function retrieve($demoId)
      {
        if (isset($this->registry[$demoId])) {
          return $this->registry[$demoId];
        }
        $demo = $this->demoFactory->create();
        $demo->load($demoId);
        if ($demo->getId() === null || $demo->getId() != $demoId)
        {
          throw NoSuchEntityException::singleField(DemoInterface::ID, $demoId);
        }
        $this->registry[$demoId] = $demo;
        return $demo;
      }
    
      /**
      * Remove an instance of the Demo Model from the registry
      *
      * @param int $demoId
      * @return void
      */
      public function remove($demoId)
      {
        unset($this->registry[$demoId]);
      }
    }
  4. Create a data interface; here, the available attributes (getters and setters) are defined:

    Api/Data/DemoInterface.php

    <?php
    namespace Genmato\Sample\Api\Data;
    
    use Genmato\Sample\Api\Data\DemoExtensionInterface;
    use Magento\Framework\Api\ExtensibleDataInterface;
    
    interface DemoInterface extends ExtensibleDataInterface
    {
    
      const ID = 'id';
      const TITLE = 'title';
      const CREATION_TIME = 'creation_time';
      const UPDATE_TIME = 'update_time';
      const IS_ACTIVE = 'is_active';
      const IS_VISIBLE = 'is_visible';
    
      /**
      * Get id
      *
      * @api
      * @return int|null
      */
      public function getId();
    
      /**
      * Set id
      *
      * @api
      * @param int $id
      * @return $this
      */
      public function setId($id);
    
      /**
      * Get Title
      *
      * @api
      * @return string
      */
      public function getTitle();
    
      /**
      * Set Title
      *
      * @api
      * @param string $title
      * @return $this
      */
      public function setTitle($title);
    
      /**
      * Get Is Active
      *
      * @api
      * @return bool
      */
      public function getIsActive();
    
      /**
      * Set Is Active
      *
      * @api
      * @param bool $isActive
      * @return $this
      */
      public function setIsActive($isActive);
    
      /**
      * Get Is Visible
      *
      * @api
      * @return bool
      */
      public function getIsVisible();
    
      /**
      * Set Is Active
      *
      * @api
      * @param bool $isVisible
      * @return $this
      */
      public function setIsVisible($isVisible);
    
      /**
      * Get creation time
      *
      * @api
      * @return string
      */
      public function getCreationTime();
    
      /**
      * Set creation time
      *
      * @api
      * @param string $creationTime
      * @return $this
      */
      public function setCreationTime($creationTime);
    
      /**
      * Get update time
      *
      * @api
      * @return string
      */
      public function getUpdateTime();
    
      /**
      * Set update time
      *
      * @api
      * @param string $updateTime
      * @return $this
      */
      public function setUpdateTime($updateTime);
    
      /**
      * Retrieve existing extension attributes object or create a new one.
      *
      * @api
      * @return DemoExtensionInterface|null
      */
      public function getExtensionAttributes();
    
      /**
      * Set an extension attributes object.
      *
      * @api
      * @param DemoExtensionInterface $extensionAttributes
      * @return $this
      */
      public function setExtensionAttributes(DemoExtensionInterface $extensionAttributes);
    }
  5. Create the data mapping class:

    Model/Data/Demo.php

    <?php
    namespace Genmato\Sample\Model\Data;
    
    use Magento\Framework\Api\AbstractExtensibleObject;
    use Genmato\Sample\Api\Data\DemoInterface;
    use Genmato\Sample\Api\Data\DemoExtensionInterface;
    
    class Demo extends AbstractExtensibleObject implements DemoInterface
    {
    
      /**
      * Get id
      *
      * @return int|null
      */
      public function getId()
      {
        return $this->_get(self::ID);
      }
    
      /**
      * Set id
      *
      * @param int $id
      * @return $this
      */
      public function setId($id)
      {
        return $this->setData(self::ID, $id);
      }
    
      /**
      * Get code
      *
      * @return string
      */
      public function getTitle()
      {
        return $this->_get(self::TITLE);
      }
    
      /**
      * Set code
      *
      * @param string $title
      * @return $this
      */
      public function setTitle($title)
      {
        return $this->setData(self::TITLE, $title);
      }
    
      /**
      * Get Is Active
      *
      * @return bool
      */
      public function getIsActive()
      {
        return $this->_get(self::IS_ACTIVE);
      }
    
      /**
      * Set Is Active
      *
      * @param bool $isActive
      * @return $this
      */
      public function setIsActive($isActive)
      {
        return $this->setData(self::IS_ACTIVE, $isActive);
      }
    
      /**
      * Get Is Visible
      *
      * @return bool
      */
      public function getIsVisible()
      {
        return $this->_get(self::IS_VISIBLE);
      }
    
      /**
      * Set Is Active
      *
      * @param bool $isVisible
      * @return $this
      */
      public function setIsVisible($isVisible)
      {
        return $this->setData(self::IS_VISIBLE, $isVisible);
      }
    
      /**
      * Get creation time
      *
      * @return string
      */
      public function getCreationTime()
      {
        return $this->_get(self::CREATION_TIME);
      }
    
      /**
      * Set creation time
      *
      * @param string $creationTime
      * @return $this
      */
      public function setCreationTime($creationTime)
      {
        return $this->setData(self::CREATION_TIME, $creationTime);
      }
    
      /**
      * Get update time
      *
      * @return string
      */
      public function getUpdateTime()
      {
        return $this->_get(self::UPDATE_TIME);
      }
    
      /**
      * Set update time
      *
      * @param string $updateTime
      * @return $this
      */
      public function setUpdateTime($updateTime)
      {
        return $this->setData(self::UPDATE_TIME, $updateTime);
      }
    
      /**
      * {@inheritdoc}
      *
      * @return DemoExtensionInterface|null
      */
      public function getExtensionAttributes()
      {
        return $this->_getExtensionAttributes();
      }
    
      /**
      * {@inheritdoc}
      *
      * @param DemoExtensionInterface $extensionAttributes
      * @return $this
      */
      public function setExtensionAttributes(DemoExtensionInterface $extensionAttributes)
      {
        return $this->_setExtensionAttributes($extensionAttributes);
      }
    }
  6. Create the search results interface; this is used in the getList command:

    Api/Data/DemoSearchResultsInterface.php

    <?php
    namespace Genmato\Sample\Api\Data;
    
    use Genmato\Sample\Api\Data\DemoInterface;
    use Magento\Framework\Api\SearchResultsInterface;
    
    interface DemoSearchResultsInterface extends SearchResultsInterface
    {
      /**
      * Get demo item list.
      *
      * @api
      * @return DemoInterface[]
      */
      public function getItems();
    
      /**
      * Set demo item list.
      *
      * @api
      * @param DemoInterface[] $items
      * @return $this
      */
      public function setItems(array $items);
    
    }
  7. Bind the interfaces through di.xml by adding the following lines:

    etc/di.xml

      <preference for="Genmato\Sample\Api\DemoRepositoryInterface"
        type="Genmato\Sample\Model\ResourceModel\DemoRepository" />
    
      <preference for="Genmato\Sample\Api\Data\DemoInterface" type="Genmato\Sample\Model\Data\Demo" />
      <preference for="Genmato\Sample\Api\Data\DemoSearchResultsInterface"
        type="Magento\Framework\Api\SearchResults" />
  8. Add the Test controller; here, we test the working of the service layer: (This is optional.)

    Controller/Index/Test.php

    <?php
    namespace Genmato\Sample\Controller\Index;
    
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;
    use Magento\Framework\Api\SearchCriteriaBuilder;
    use Genmato\Sample\Api\DemoRepositoryInterface;
    use Genmato\Sample\Model\Data\DemoFactory;
    use Genmato\Sample\Api\Data\DemoInterface;
    
    class Test extends Action
    {
      /**
      * @var PageFactory
      */
      private $resultPageFactory;
    
      /** @var  DemoRepositoryInterface */
      private $demoRepository;
    
      /** @var DemoFactory  */
      private $demo;
    
      /**
      * @var SearchCriteriaBuilder
      */
      private $searchCriteriaBuilder;
      /**
      * @param Context $context
      * @param PageFactory $resultPageFactory
      * @param DemoRepositoryInterface $demoRepository
      * @param SearchCriteriaBuilder $searchCriteriaBuilder
      * @param DemoFactory $demoFactory
      */
      public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        DemoRepositoryInterface $demoRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        DemoFactory $demoFactory
      )
      {
        $this->demoRepository = $demoRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->demo = $demoFactory;
        parent::__construct($context);
      }
    
      /**
      * Renders Sample
      */
      public function execute()
      {
        echo '<pre>';
    
        // Create new record through service layer/contract
    
        /** @var DemoInterface $demoRecord */
        $demoRecord = $this->demo->create();
        $demoRecord->setIsActive(1)
          ->setIsVisible(1)
          ->setTitle('Test through Service Layer');
    
        $demo = $this->demoRepository->save($demoRecord);
        print_r($demo);
    
        // Get list of available records
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $searchResult = $this->demoRepository->getList($searchCriteria);
        foreach ($searchResult->getItems() as $item) {
          echo $item->getId().' => '.$item->getTitle().'<br>';
        }
      }
    
    }
  9. Refresh the cache and generated data:
    bin/magento setup:upgrade
    
  10. Access the result of the test URL:

    http://example.com/sample/index/test/

How it works…

The service layer/contract defines the methods and data format through these interfaces.

DemoRepositoryInterface

This interface describes the available methods, input, and output that is expected. The actual logic for the methods is done by the Model\ResourceModel\ DemoRepository class. In di.xml, there is a preference created that will use DemoRepository instead of the Interface class:

<preference for="Genmato\Sample\Api\DemoRepositoryInterface"
  type="Genmato\Sample\Model\ResourceModel\DemoRepository" />

DemoInterface

In this interface, the available getters and setters for the object are described. Just like RepositoryInterface, the actual processing of the data is mapped through di.xml to the Data\Demo class:

<preference for="Genmato\Sample\Api\Data\DemoInterface" type="Genmato\Sample\Model\Data\Demo" />

See also

In the next recipe, we will see how the getById, deleteById, list, and save methods can be used in your own code.