C
C
camradee2022-02-10 18:08:46
Magento
camradee, 2022-02-10 18:08:46

Why doesn't Dependency Injection work in Magento 2?

Added a helper class for working with products, the constructor looks like this:

public function __construct(\Magento\Catalog\Model\ProductRepository $repository = null)
    {
        $this->_repository = $repository ?: $this->_objectManager->create('\Magento\Catalog\Model\ProductRepository');
   }

We have to check and initialize $repository because nothing is injected. I just understand Magenta and I don’t understand what’s wrong, maybe I need to write something in the xml file somewhere. Where is the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
camradee, 2022-02-11
@camradee

Here is an example of a simple injection:
https://www.mageplaza.com/blog/the-basics-of-depen...
I made the Data and HelperDemo classes following the example, the injection does not work, writes:
"ArgumentCountError: Too few arguments to function DependentClass ::__construct(), 0 passed...."

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question