AdobeDocs/commerce-php

Cannot instantiate interface Dev\RestApi\Api\ProductRepositoryInterface

Closed this issue · 4 comments

Expected Behaviour

I have followed these steps to learn the Rest API. I have created a Dev_RestApi module and copied the exact same code in it. I have not made any single line of code change. Now I am requesting the API to get the product by id in the Postman and I am getting below error.

Fatal error: Uncaught Error: Cannot instantiate interface Dev\RestApi\Api\ProductRepositoryInterface in /Applications/MAMP/htdocs/local/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:121

Actual Behaviour

The API call should return the product information based on given Id.

Reproduce Scenario (including but not limited to)

Create a module using this documentation and request the api call to get the product details.
https://developer.adobe.com/commerce/php/tutorials/backend/create-custom-rest-api/

Steps to Reproduce

Create a module using this documentation and request the api call to get the product details.
https://developer.adobe.com/commerce/php/tutorials/backend/create-custom-rest-api/

Platform and Version

Magento 2.3.4-p1
PHP 7.3

Sample Code that illustrates the problem

I have attached the zip of the Dev_ResetApi module which I have created and the screenshot of the API request in the Postman.

Logs taken while reproducing problem

Screenshot 2022-07-26 at 11 16 54 AM

[Dev.zip](https://github.com/AdobeDocs/commerce-php/files/9186941/Dev.zip)

@jeff-matthews Can you please check issue? Please help here.

@keharper Can you take a look at this?

This step is missing in the doc. Please follow this to resolve the issue.

Create di.xml in app/code/Dev/RestApi/etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Dev\RestApi\Api\ProductRepositoryInterface" type="Dev\RestApi\Model\Api\ProductRepository"/>
    <preference for="Dev\RestApi\Api\RequestItemInterface" type="Dev\RestApi\Model\Api\RequestItem"/>
    <preference for="Dev\RestApi\Api\ResponseItemInterface" type="Dev\RestApi\Model\Api\ResponseItem"/>
</config>

@RonakParmar, please check the solution provided by @chakravarthykalyan90 and let us know if it resolves your issue.