Uncaught Error: Cannot instantiate interface

juanMay發表於2021-03-15

在新建 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

相關文章