cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot instantiate interface by using AbstractFactory

Cannot instantiate interface by using AbstractFactory

Preconditions

Magento 2.3.4 EE
Windows
xampp7.2.28

 

 

I've created a module Training_IpApi, after compile and go to the browser I receive this error shown below:

 

 

Fatal error: Uncaught Error: Cannot instantiate interface Training\IpApi\Api\Data\LocationDTOInterface in 
C:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php:121 Stack trace: #0 
C:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\Compiled.php(108): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Training\\IpApi\\...', Array) #1 
C:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\ObjectManager.php(56): Magento\Framework\ObjectManager\Factory\Compiled->create('Training\\IpApi\\...', Array) #2 
C:\xampp\htdocs\magento2\generated\code\Training\IpApi\Api\Data\LocationDTOInterfaceFactory.php(43): Magento\Framework\ObjectManager\ObjectManager->create('\\Training\\IpApi...', Array) #3 
C:\xampp\htdocs\magento2\app\code\Training\IpApi\Model\VisitorLocation.php(52): Training\IpApi\Api\Data\LocationDTOInterfaceFactory->create(Array) #4 
C:\xampp\htdocs\magento2\app\code\Training\IpApi\Controller\Index\Index.php(22): Training\IpApi\Model\Visit in C:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php on line 121

 

 

According to the description of error, I checked the interface factory (LocationDTOInterfaceFactory) which has been auto-generated by Magento in the generated/ folder, the code shows below:

 

<?php
namespace Training\IpApi\Api\Data;

/**
* Factory class for @see \Training\IpApi\Api\Data\LocationDTOInterface
*/
class LocationDTOInterfaceFactory
{
/**
* Object Manager instance
*
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager = null;

/**
* Instance name to create
*
* @var string
*/
protected $_instanceName = null;

/**
* Factory constructor
*
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param string $instanceName
*/
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\Training\\IpApi\\Api\\Data\\LocationDTOInterface')
{
$this->_objectManager = $objectManager;
$this->_instanceName = $instanceName;
}

/**
* Create class instance with specified parameters
*
* @param array $data
* @return \Training\IpApi\Api\Data\LocationDTOInterface
*/
public function create(array $data = [])
{
return $this->_objectManager->create($this->_instanceName, $data);
}
}

 

The relative module core class is:

 

<?php
namespace Training\IpApi\Model;

use Training\IpApi\Api\VisitorLocationInterface;

class VisitorLocation implements VisitorLocationInterface
{

private $ipApi;

protected $remoteAddress;

private $json;

private $locationDTOFactory;

public function __construct(
\Training\IpApi\Api\IpApiInterface $ipApi,
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
\Magento\Framework\Serialize\Serializer\Json $json,
\Training\IpApi\Api\Data\LocationDTOInterfaceFactory $locationDTOFactory
)
{
$this->ipApi = $ipApi;

$this->json = $json;
$this->locationDTOFactory = $locationDTOFactory;
$this->remoteAddress = $remoteAddress;
}

public function getAreas()
{
$ipAddress= $this->remoteAddress->getRemoteAddress();
print_r($ipAddress);
$ipAddress = "9.9.9.9";
$data = $this->ipApi->lookup($ipAddress);
$jsonData = $this->json->unserialize($data);

print_r($jsonData);
echo '<br>';
$country = "US";
$region = "NY";

if ($jsonData["status"] == "success") {
$country = $jsonData['country'];
$region = $jsonData['regionName'];
}

print_r($region);
$locationDTO = $this->locationDTOFactory->create([
"country" => $country,
"region" => $region
]);

return $locationDTO;
}
}

 

also the di.xml is here:

 

<?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 = "Training\IpApi\Api\VisitorLocationInterface" type="Training\IpApi\Model\VisitorLocation"/>
<preference for = "Training\IpApi\Api\IpApiInterface" type="Training\IpApi\Model\IpApi"/>
<preference for = "Training\IpApi\Api\LocationDTOInterface" type="Training\IpApi\Model\Data\LocationDTO"/>
<type name="Training\IpApi\Model\Data\LocationDTO">
<arguments>
<argument name="country" xsi:type="string">US</argument>
<argument name="region" xsi:type="string">CA</argument>
</arguments>
</type>
</config>

 

This is a simple trial of the magento module for study, so it should not be very complicated, but I still do not know how to fix this problem. 

I've tried some method like (not in this order):

  • clean generated/ and var/cache folder" 
  • php bin/magento setup:upgrade
  • php bin\magento cache:flush
  • php bin\magento setup:di:compile

I have tried to combine those operation to complete a process which seems like to be enable to work for Magento, but it finally did not work.

Could someone give me some advice that really helps me according to my code? I'm glad to give some additional information if you want. Moreover, I also need some debug method, if you have, for Magento to prevent from the puzzle like this, thanks! 

1 REPLY 1

Re: Cannot instantiate interface by using AbstractFactory

Clear Redis Cache is helpful to me

 

redis-cli flushall