4

ItemInterface:

namespace MyVendor\SampleModule1\Api\Data;

interface ItemInterface
{
    /**
     * @return string
     */
    public function getName();

    /**
     * @return string|null
     */
    public function getDescription();
}

ItemRepositoryInterface:

<?php

namespace MyVendor\SampleModule1\Api;

use MyVendor\SampleModule1\Api\Data\ItemInterface;

interface ItemRepositoryInterface
{
    /**
     * @return ItemInterface[]
     */
    public function getList();
}

ItemRepository:

<?php

namespace MyVendor\SampleModule1\Model;

use MyVendor\SampleModule1\Api\ItemRepositoryInterface;
use MyVendor\SampleModule1\Model\ResourceModel\Item\CollectionFactory;

class ItemRepository implements ItemRepositoryInterface
{
    private $collectionFactory;

    public function __construct(CollectionFactory $collectionFactоry)
    {
        $this->collectionFactory = $collectionFactоry;
    }

    public function getList()
    {
        return $this->collectionFactory->create()->getItems();
    }
}

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">
    <type name="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="myvendorAddItem" xsi:type="object">MyVendor\SampleModule1\Console\Command\AddItem</item>
            </argument>
        </arguments>
    </type>
    <preference for="MyVendor\SampleModule1\Api\Data\ItemInterface" type="MyVendor\SampleModule1\Model\Item"/>
    <preference for="MyVendor\SampleModule1\Api\ItemRepositoryInterface" type="MyVendor\SampleModule1\Model\ItemRepository"/>
</config>

etc/webapi.xml

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/myvendor" method="GET">
        <service class="MyVendor\SampleModule1\Api\ItemRepositoryInterface" method="getList" />
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>
</routes>

I ran the commands:

magento cache:flush
magento cache:clean
magento setup:di:compile
magento setup:upgrade

When i access the url http://mymage.com/rest/V1/myvendor i get the fallowing error:

<message>Class ItemInterface does not exist</message>
<code>-1</code>
<trace>
#0 /var/www/html/lib/internal/Magento/Framework/Reflection/MethodsMap.php(149): ReflectionClass->__construct('ItemInterface') #1.... 

EDIT:

Item.php model:

<?php

namespace MyVendor\SampleModule1\Model;

use Magento\Framework\Model\AbstractModel;

class Item extends AbstractModel
{
    protected function _construct()
    {
        $this->_init(\MyVendor\SampleModule1\Model\ResourceModel\Item::class);
    }
}

I just don't understand where is the problem ? ? ?

2
  • where is your MyVendor\SampleModule1\Model\Item.php file? Commented Sep 26, 2017 at 11:46
  • i will edit the questions 1 sec Commented Sep 26, 2017 at 11:47

3 Answers 3

1

So, basically the ItemRepositoryInterface should look like:

<?php

namespace MyVendor\SampleModule1\Api;

interface ItemRepositoryInterface
{
    /**
     * @return \MyVendor\SampleModule1\Api\Data\ItemInterface[]
     */
    public function getList();
}

... I have no words .

1

ItemRepositoryInterface:

The error message Class ItemInterface does not exist, provide the the namespace of ItemInterface in the Doc Block section.

In the ItemRepositoryInterface Doc Block is very very important and it should be

/** * @return \MyVendor\SampleModule1\Api\Data\ItemInterface[] */

See complete code below...

namespace MyVendor\SampleModule1\Api;

use MyVendor\SampleModule1\Api\Data\ItemInterface;

interface ItemRepositoryInterface
{
    /**
     * @return \MyVendor\SampleModule1\Api\Data\ItemInterface[]
     */
    public function getList();
}
0

Replace item.php file with below content,

<?php

namespace MyVendor\SampleModule1\Model;

use Magento\Framework\Model\AbstractModel;

class Item extends AbstractModel
{
    protected function _construct()
    {
        $this->_init(\MyVendor\SampleModule1\Model\ResourceModel\Item::class);
    }

    /**
     * Get name
     *
     * @return string|null
     */
    public function getName()
    {
        return parent::getData('name');   
    }

    /**
     * Set name
     *
     * @param string $name
     * @return ItemInterface
     */
    public function setName($name)
    {
        return $this->setData('name', $name);
    }

    /**
     * Get Description
     *
     * @return string|null
     */
    public function getDescription()
    {
        return parent::getData('description');   
    }

    /**
     * Set Description
     *
     * @param string $description
     * @return ItemInterface
     */
    public function setDescription($description)
    {
        return $this->setData('description', $description);
    }
}
4
  • same error... nothing is changed Commented Sep 26, 2017 at 12:53
  • have you remove var/generation folder? Commented Sep 26, 2017 at 12:57
  • doesnt cache:flush and setup:upgrade do that ? Commented Sep 26, 2017 at 13:03
  • isn't this a di problem, not Model/Method structure one Commented Sep 26, 2017 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.