未捕获的错误:无法实例化接口,其中接口已在类中使用

问题描述 投票:0回答:2

我有一个名为 ProductUpdateInterface 的接口,代码如下

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(

    \Magento\Framework\Component\ComponentRegistrar::MODULE,

    'AdmNetsuitePQ_NetsuitePQ',

    __DIR__

);

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/Model/ProductUpdate.php

<?php
namespace AdmNetsuitePQ\NetsuitePQ\Model;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ProductFactory;
use AdmNetsuitePQ\NetsuitePQ\Api\ProductUpdateInterface;

class ProductUpdate implements ProductUpdateInterface
{
    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function update($productId, $price = null, $quantity = null)
    {
        $productId = (int) $productId;
        $product = $this->productRepository->getById($productId);
        if (!$product->getId()) {
            throw new NoSuchEntityException(__('Product with ID %1 not found.', $productId));
        }
        if ($price !== null && $price < 0) {
            throw new InputException(__('Price must be greater than or equal to 0.'));
        }
        if ($quantity !== null && $quantity < 0) {
            throw new InputException(__('Quantity must be greater than or equal to 0.'));
        }
        if ($price !== null) {
            $product->setPrice($price);
        }
        if ($quantity !== null) {
            $product->setQty($quantity);
            $product->setIsQtyDecimal(false);
        }
        $this->productRepository->save($product);
        return true;
    }
}

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/Api/ProductUpdateInterface.php

<?php
namespace AdmNetsuitePQ\NetsuitePQ\Api;

interface ProductUpdateInterface
{
    /**
     * Update product quantity and price
     *
     * @param int $productId
     * @param float|null $price
     * @param float|null $quantity
     * @return bool
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Exception\StateException
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function update($productId, $price = null, $quantity = null);
}

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/etc/module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

    <module name="AdmNetsuitePQ_NetsuitePQ" setup_version="1.0.0"> </module>

</config>

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/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/product/update" method="POST">
        <service class="AdmNetsuitePQ\NetsuitePQ\Api\ProductUpdateInterface" method="update"/>
        <resources>
            <resource ref="Magento_Catalog::products"/>
        </resources>
        <data>
            <parameter name="productId" force="true">123</parameter>
            <parameter name="price" force="false">12.5</parameter>
            <parameter name="quantity" force="false">20</parameter>
        </data>
    </route>
</routes>

现在当我通过邮递员发送数据时

POST /rest/V1/product/update HTTP/1.1
Host: my_site.com
Content-Type: application/json
Authorization: OAuth oauth_consumer_key="j9q7uilt4trla8hsi5lo1gtbtuh46icr",oauth_token="df4lz1gasp0su25rbwnp225kxfwknehn",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1678733662",oauth_nonce="kfxspsm3a2n",oauth_version="1.0",oauth_signature="sdKx5cO%2BU7fePuKPWfgZy5ZCAks%3D"
Cookie: PHPSESSID=23vl60opom23c1gfqalnh914e9
Content-Length: 65

{
    "productId": 123,
    "price": 12.5,
    "quantity": 20
}

现在当我执行这些时(实际上这些是 Magento2 模块中的代码)我得到以下错误

致命错误:未捕获错误:无法实例化接口 AmdNetsuitePQ\NetsuitePQ\Api\ProductUpdateInterface

请帮忙解决这个错误

php oop interface magento2
2个回答
0
投票

您需要告诉 magento 给定接口的默认类是什么,以便能够在您的 webapi 定义中使用该接口。

AdmNetsuitePQ/NetsuitePQ/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="AdmNetsuitePQ\NetsuitePQ\Api\ProductUpdateInterface" 
        type="AdmNetsuitePQ\NetsuitePQ\Model\ProductUpdate"
    />
</config>

0
投票

“Cannot instantiate interface”的错误通常表示代码中使用的接口不能直接实例化,因为它只定义了方法签名,没有任何实现。

要解决此错误,您需要确保您在代码中使用的是接口的实际实现类。在这种情况下,您应该在 webapi.xml 文件中使用 ProductUpdate 类,而不是 ProductUpdateInterface 接口。

为此,请替换 webapi.xml 中的以下行:

<service class="AdmNetsuitePQ\NetsuitePQ\Api\ProductUpdateInterface" method="update"/>

与:

<service class="AdmNetsuitePQ\NetsuitePQ\Model\ProductUpdate" method="update"/>

此更改将使用 ProductUpdate 类作为 ProductUpdateInterface 接口的实现类,并且应该允许代码正常运行。

© www.soinside.com 2019 - 2024. All rights reserved.