I wrote a custom api for magento 2.3.1
When i call the endpoint that i was created i get the following error
{ "messages": { "error": [ { "code": 500, "message": "Server internal error. See details in report api/1560842508114" } ] } } Report detail:
"Fatal Error: 'Uncaught Error: Cannot instantiate interface Demo\Hello\Api\HelloInterface in /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:116\nStack trace:\n#0 /var/www/html/vendor/magento/framework/ObjectManager/Factory/Compiled.php(108): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Demo\\Hello\\Api\\...', Array)\n#1 /var/www/html/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Compiled->create('Demo\\Hello\\Api\\...')\n#2 /var/www/html/vendor/magento/module-webapi/Controller/Rest/SynchronousRequestProcessor.php(90): Magento\Framework\ObjectManager\ObjectManager->get('Demo\\Hello\\Api\\...')\n#3 /var/www/html/vendor/magento/module-webapi/Controller/Rest.php(188): Magento\Webapi\Controller\Rest\SynchronousRequestProcessor->process(Object(Magento\Framework\Webapi\Rest\Request\Proxy))\n#4 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Fr' in '/var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php' on line 116"
app/code/Demo/Hello/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Demo_Hello', __DIR__ ); app/code/Demo/Hello/Api/HelloInterface.php
<?php namespace Demo\Hello\Api; interface HelloInterface { /** * Returns greeting message to user * * @api * @param string $name Users name. * @return string Greeting message with users name. */ public function name($name); } app/code/Demo/Hello/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="Demo\Hello\Api\HelloInterface" type="Demo\Hello\Model\Hello" /> </config> app/code/Demo/Hello/Model/Hello.php
<?php namespace Demo\Hello\Model; use Demo\Hello\Api\HelloInterface; class Hello implements HelloInterface { /** * Returns greeting message to user * * @api * @param string $name Users name. * @return string Greeting message with users name. */ public function name($name) { return "Hello, " . $name; } }
app/code/Demo/etc/di.xmlandapp/code/Demo/registration.php