Uncaught Error: Cannot instantiate interface

在新建 May\Applet\Api\SashareInterface 接口,去实现salogin 方法,用 postman 调用接口时报错,报错内容如下:

"Fatal Error: 'Uncaught Error: Cannot instantiate interface May\\Applet\\Api\\SashareInterface in \/Users\/project\/practise\/mayMagento2.3\/vendor\/magento\/framework\/ObjectManager\/Factory\/AbstractFactory.php:116\nStack trace:\n#0 \/Users\/project\/practise\/mayMagento2.3\/vendor\/magento\/framework\/ObjectManager\/Factory\/Compiled.php(108): Magento\\Framework\\ObjectManager\\Factory\\AbstractFactory->createObject('May\\\\Applet\\\\Api\\\\...', Array)\n#1 \/Users\/project\/practise\/mayMagento2.3\/vendor\/magento\/framework\/ObjectManager\/ObjectManager.php(70): Magento\\Framework\\ObjectManager\\Factory\\Compiled->create('May\\\\Applet\\\\Api\\\\...')\n#2 \/Users\/project\/practise\/mayMagento2.3\/vendor\/magento\/module-webapi\/Controller\/Rest\/SynchronousRequestProcessor.php(90): Magento\\Framework\\ObjectManager\\ObjectManager->get('May\\\\Applet\\\\Api\\\\...')\n#3 \/Users\/project\/practise\/mayMagento2.3\/vendor\/magento\/module-webapi\/Controller\/Rest.php(188): Magento\\Webapi\\Controller\\Rest\\SynchronousRequestProcessor->process(Object(Magento\\Framework\\Webapi\\Rest\\Request\\Proxy))\n#4 \/Users\/pr' in '\/Users\/project\/practise\/mayMagento2.3\/vendor\/magento\/framework\/ObjectManager\/Factory\/AbstractFactory.php' on line 116"

经过数据排查,发现忘了添加 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">
    <!-- Sashare -->
    <preference for="May\Applet\Api\SashareInterface" type="May\Applet\Model\Api\Sashare"/>
</config>

执行下面命令:

- 清除 generated
- 清除 var/cache
- php bin/magento cache:clean  清空缓存 
- php bin/magento cache:flush  刷新缓存
- php bin/magento setup:di:compile 生成自动类文件 

执行上述命令,接口请求正常!

本作品采用《CC 协议》,转载必须注明作者和本文链接
May
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!