Hi ,
I am developing an API to get product by their ID. Here is the files i created so far.
registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MyCompany_ProductBy', __DIR__ );
Mycompany/ProductBy/API/ProductByInterface.php
<?php namespace MyCompany\ProductBy\Api; interface ProductByInterface { /** * Returns product details. * * @param int $id * @return array[] */ public function getProductById($id); }
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="MyCompany\ProductBy\Api\ProductByInterface" type="MyCompany\ProductBy\Model\ProductBy" /> </config>
etc/module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="MyCompany_ProductBy" setup_version="2.0.0"> </module> </config>
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"> <route url="/V1/productbyid/:id" method="GET"> <service class="MyCompany\ProductBy\Api\ProductByInterface" method="getProductById"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>
Model/ProductBy.php
<?php namespace MyCompany\ProductBy\Model; use MyCompany\ProductBy\Api\ProductByInterface; use Magento\Framework\Controller\ResultFactory; class ProductBy implements ProductByInterface { /** * @var \Magento\Catalog\Api\ProductRepositoryInterface */ protected $productRepository; protected $resultJsonFactory; public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory) { $this->productRepository = $productRepository; $this->resultJsonFactory = $resultJsonFactory; } /** * {@inheritdoc} */ public function getProductById($id) { $prod_data = $this->productRepository->getById($id); $color_option_id = $prod_data->getColor(); $attr = $prod_data->getResource()->getAttribute('color'); if ($attr->usesSource()) { $coloroptionText = $attr->getSource()->getOptionText($color_option_id); } $size_option_id = $prod_data->getShirtSize(); $attr = $prod_data->getResource()->getAttribute('shirt_size'); if ($attr->usesSource()) { $sizeoptionText = $attr->getSource()->getOptionText($size_option_id); } //return $this->resultJsonFactory->create()->setData(['data'=>$prod_data]); $result = $this->resultJsonFactory->create(); /** You may introduce your own constants for this custom REST API */ $result->setHeader('Content-Transfer-Encoding', 'binary', true); $result->setData($prod_data->getData()); return $result; } }
When i execute the rest api , i am getting the following error in magento exception log.
main.CRITICAL: Report ID: webapi-5bea6f9f1c395; Message: Class does not exist {"exception":"[object] (Exception(code: -1): Report ID: webapi-5bea6f9f1c395; Message: Class does not exist at /var/www/html/myapp/vendor/magento/framework/Webapi/ErrorProcessor.php:205, ReflectionException(code: -1): Class does not exist at /var/www/html/myapp/vendor/magento/framework/Reflection/MethodsMap.php:155)"} []
Can anybody tell me what i am wrong here. I googled the error and tried to mention the return type array , Model/ProductInterface , null . But none of them worked out.
Things break when you have a return type of 'array', or in your case 'array[]'.
Try changing the return type from 'array[]' to 'int[]' in the PHPDocs for Mycompany/ProductBy/API/ProductByInterface.php or if you can't ensure it will be an array of integers, you could use 'string[]' or 'mixed[]' depending on the actual return type.
Hope this helps.