0

I'm using Ubuntu 18.04 with PHP 7.3.14. I was in my root directory and entered the command bin/magento setup:install and got this error. What is going on?

PHP Fatal error:  Uncaught Error: Cannot instantiate interface Project\Crawler\Api\CrawlerItemRepositoryInterface in /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:50
    Stack trace:
    #0 /var/www/html/root/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Project\\Crawler...')
    #1 /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(160): Magento\Framework\ObjectManager\ObjectManager->get('Project\\Crawler...')
    #2 /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(246): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Project\\Crawler...', NULL, 'crawlerItemRepo...', 'Project\\ImageCr...')
    #3 /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime('Project\\ImageCr...', Array, A in /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php on line 50

I have been looking at these but they didn't work for me:

3 Answers 3

4

Magento in attempting to instantiate the interface, interfaces cannot be instantiated. You need a class that implements the interface.

To do this use a preference in the di.xml that suggests which class to use for the CrawlerItemRepositoryInterface

1
  • Ow yea you are right. Nvm my answer Commented Feb 10, 2020 at 16:21
1

Well it does not find that interface or can not load it and that can have multiple reasons.

Here a couple :

  1. Your interface does not exist
  2. The interface has the wrong namespace
  3. The interface is in the wrong directory structure
  4. The interface wrong syntaxes
  5. The interface is opened with a <? tag instead of a <?php tag and your php settings does not allow shorthand php opening tags

EDIT

See Netstorm's answer. Will leave this here for debugging purposes for other people

0

ADDITIONALLY to a correct di.xml

In Magento 2.4.5-p1.
I found that dependency between files with the SAME $this->variable name CAN cause this issue.

Solution: Change the variable name from

$this->mymodelFactory = $mymodelFactory;

To

$this->_mymodelFactory = $_mymodelFactory;

.
.
.
Then:
Error > Cannot instantiate interface

Vendor/Module/Model/Order.php

protected MymodelFactory $mymodelFactory;

_ _construct(
... 
MymodelRepositoryInterface $mymodelRepository,
MymodelFactory $mymodelFactory
){
$this->mymodelRepository = $mymodelRepository;
$this->mymodelFactory = $mymodelFactory;
}

di.xml

<preference for="Vendor\Module\Api\MymodelRepositoryInterface" type="Vendor\Module\Model\MymodelRepository"/>

Vendor/Module/Model/MymodelRepository.php

_ _construct(
... 
MymodelFactory $mymodelFactory
){
$this->mymodelFactory = $mymodelFactory
}

.
.
.
NOW:
No errors

Vendor/Module/Model/Order.php

protected MymodelFactory $_mymodelFactory;

_ _construct(
... 
MymodelRepositoryInterface $mymodelRepository,
MymodelFactory $_mymodelFactory
){
$this->mymodelRepository = $mymodelRepository;
$this->_mymodelFactory = $_mymodelFactory;
}

di.xml

<preference for="Vendor\Module\Api\MymodelRepositoryInterface" type="Vendor\Module\Model\MymodelRepository"/>

Vendor/Module/Model/MymodelRepository.php

_ _construct(
... 
MymodelFactory $mymodelFactory
){
$this->mymodelFactory = $mymodelFactory
}

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.