Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [dependency-injection]

Denotes questions regarding Magento 2 Dependency injection

1
vote
1answer
24 views

Dependancy injection and shared/unshared object and cron job

Something interesting happens to Magento2 code and I cannot realise what I missed. The same code works flawlessly via browser and does not work via cron. (in adminhtml area) I calculate the result in ...
4
votes
2answers
34 views

What exactly is Dependency Injection in Magento?

I'm learning Magento 2 and couldn't able to understand some of the below things Dependency Injection Javascript bundling (Because some suggest to enable and some don't) Third party module override ...
1
vote
1answer
30 views

magento 2 how dependency injection worked

I know this question is asked many time by another user but all of them describe "how we can use magento DI" ? they does not speak about how magento pass object that constructor method needed or how ...
0
votes
0answers
17 views

Context dependency in Magento2 [duplicate]

I have my custom module in which I am using Magento\Framework\App\Action\Context is a dependency in my Controller because Magento\Framework\App\Action\Action needs it. but I don't understand why we ...
0
votes
0answers
26 views

Magento 2 circular dependancy error on upgrade

I'm currently running a website on Magento 2.2.7 When we add any new module and run the php bin/magento setup:upgrade command the admin panel immediately stops working and gives us an error beginning ...
0
votes
2answers
30 views

Magento 2 Model Classses are not injecting without Factory in custom module

I am developing an module in Magento 2.I have created helper,Model Fatal error:vendorname\modulename\Model\Testclass class is not exist.I have tried each and everything to resolve this issue but ...
1
vote
2answers
27 views

DI not working as expected

I'm trying to get an instance of Magento\Cms\Api\GetBlockByIdentifier, however I am receiving a value of null. I notice the CMS module defines a preference for this interface vendor/magento/module-...
2
votes
1answer
31 views

Magento 2: best practice building classes

I need some advice how to structure my code in magento 2. I am working on an ERP interface to import and export products, customers and orders. So I need some helper classes to take care of some ...
2
votes
1answer
171 views

Get category name by ID

This is my Vendor/Module/Block/BlockFile.php <?php namespace Vendor\Module\Block; class CategoryList extends \Magento\Framework\View\Element\Template { public function getId() { return ...
0
votes
0answers
13 views

Trouble understanding Magento 2 context DI order in core code

In the module-customer/Controller/Adminhtml/Group , the Context class is passed into the __construct method 3rd to last, before the resultForwardFactory and resultPageFactory factory classes. ...
5
votes
1answer
392 views

How to bypass CSRF validation for certain requests like payment gateway webhook?

I am trying to integrate CC avenue payment method with Magento 2.3 however once payment is processed, user is navigated back to website with error "Invalid Form Key. Please refresh the page." Upon ...
0
votes
0answers
34 views

Magento 2 getting interface implementation from admin config

I have an interface which gives me the country in which the customer is located. interface CustomerLocatorInterface { public function getLocalCountryCode(); } And I have two possible ...
0
votes
1answer
70 views

What is the Use of Type and Virtual Types(di.xml) in Magento2?

I want to know clear about Type and Virtual Types in di.xml with examples. When we can use it. Please share simple example to understand concept. Note please go through the practical examples.
0
votes
1answer
48 views

How many types of Injection methods does Magento 2 support?

What types of Injection methods does Magento 2 support? I am aware about, Constructor Injection - consider as a Injection. Method Injection - consider as a Injection. BUT And Interface and Model ...
0
votes
2answers
490 views

magento 2: Hide shipping method in frontend

I have the shipping method "Flatrate" enabled in backend. So, as expected, it is available in frontend, too. But this shipping method should only be available in backend. Of course, I already googled ...
1
vote
0answers
150 views

Circular dependency: Magento\Backend\Helper\Data depends on Magento\Backend\Model\Url and vice versa

I got error in magento 2 like Circular dependency: Magento\Backend\Helper\Data depends on Magento\Backend\Model\Url and vice versa. clearing var/di, var/generation and var/cache didn't help. ...
0
votes
0answers
23 views

How to extend Advanced search result page's listing items

I have a module which a bit extends an appearance of listing items. It adds different labels to some products depends on various conditions. Everything works well on categories' listing pages and ...
0
votes
1answer
118 views

Magento 2: How to inject classes in custom script

I am working on some custom scripts to import some data before a new store is launched. These scripts are needed only once and will not play any role after launch. They are supposed to be executed on ...
0
votes
1answer
181 views

Weird error after Magento 2.2.6 update - Magento does not create ImageFactory class (and others)

After each an every try to update a particular Magento 2.2.5 installation (CE) to 2.2.6 we get the following error: PHP message: PHP Fatal error: Uncaught Error: Call to undefined method Magento\...
0
votes
1answer
30 views

Can I call addFieldToFilter via di.xml? / Set a global filter on product collection

I am working with vendor/smile/elasticsuite/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Fulltext/Collection.php which extends the \Magento\Catalog\Model\ResourceModel\Product\...
2
votes
1answer
480 views

Exception #0 (BadMethodCallException): Missing required argument $data

I have a module which is rewriting core module (module-fedex), the constructor is the following: public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \...
0
votes
1answer
282 views

Magento2: dependency injection issue in passing parameter to constructor

I have faced this issue many times, but unable to understand why? what is the (logic/concept/approach) behind injecting any object. i have injected few parameter to my save controller, before adding ...
0
votes
0answers
12 views

Do Magento 2 DI Helpers persist state across or through requests?

I was hoping to double-check my sanity around Magento's DI mechanism. Let's say I have X helper that is used in 3 places within the workflow of a single request, where X helper has multiple ...
0
votes
1answer
190 views

DI Compile fails with “Area code not set” error

I'm trying to use the \Magento\Sales\Model\InvoiceOrder class to invoice some orders. If I include the class in my constructor, however, then I get the "Area code not set: Area code must be set before ...
0
votes
1answer
38 views

Observer calling class in construct 500 error

I have an observer, which is working fine (after some effort on what is a Frankenstein website). However when I attempt to use my class in my construct it causes a 500 error (mode is developer, but no ...
1
vote
1answer
23 views

Injecting object into a pluigin

I just want to inject an object into my plugin. /* @var \Magento\Checkout\Model\Session */ protected $session; public function __construct(\Magento\Checkout\Model\Session $session) { $this->...
1
vote
1answer
128 views

Magento 2 : injecting models, interfaces in constructor magento

I'm new to magento 2 and I'm trying to understand how exactly injections work in magento. I understand that injecting a model and interfaces is the right approach in magento 2. But for some reason ...
3
votes
2answers
231 views

Magento 2: Does dependency injection affect performance?

I was wondering if dependency injection can be bad for the performance of the website. For example: Let's say that I'm building a helper class for my module and it contains a lot of useful methods ...
0
votes
2answers
678 views

Fatal error: Uncaught Error: Call to undefined method getDirectoryRead()

I have a controller that works and does what i need it to do if i manually add a path like: $mediaImages = '/magento/pub/media/images/'; But i cant get the DirectoryList injection to work, it returns ...
5
votes
2answers
2k views

Magento 2: How to Add a viewModel to a core block?

I am trying to overwrite a core template and add my custom logic using a viewModel as follow: <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...
0
votes
1answer
501 views

Magento2 extend core block constructor

Let's say I have a helper class (let's call it \Vendor\Module\Helper\Cart) which I'd like to use in a block. I understand I should add the helper class in block constructor, so I should extend core ...
0
votes
1answer
104 views

Magento 2 Category Collection doesn't show items not in Top Menu

I am trying to display certain categories along with their images on homepage. I've made sure the categories dont appear in the top menu and only in the code that I am trying to use. Below code only ...
0
votes
0answers
146 views

Magento 2 : When running integration tests, how can I override some of the dependencies that are injected?

I am running an integration test for Magento 2, but I am at a certain point that my integration test needs a different dependency than my production code. Without going too much into detail as to why, ...
0
votes
1answer
141 views

Magento Vagrant installation does not generate classes

I have switched to windows for a new project, and just setup Magento 2 using this vagrant box: https://github.com/paliarush/magento2-vagrant-for-developers I have it working, it is displaying ...
0
votes
1answer
347 views

Magento2 - controller redirect using plugins

Doing url redirects is an easy thing rewriting a controller class, as $this->resultRedirectFactory is accessible as it is one of default injected objects But, can we use this in plugins? I've been ...
0
votes
1answer
55 views

Inject class for overridded class

I override the \Magento\Catalog\Block\Product\View it's working but I am trying to inject class means it's not working please tell me what I did wrong and tell me how to do this without injecting the ...
2
votes
0answers
124 views

Speed up dependency injector compile

Since I have started developing with Magento 2 I've noticed, compiling the dependency injector with bin/magento setup:di:compile can take 40 seconds and up to 1 min 20s with xdebug enabled. I have ...
3
votes
0answers
80 views

Why is there a DI preference for ProductInterface?

The ProductInterface has a DI preference leading it the ProductModel. While ProductModel is marked as falling under the Backward Compatibility Standard (it is marked with @api), it is still belonging ...
0
votes
0answers
305 views

How to change the type of a constructor parameter through dependency injection

We are facing the following situation: We use a module that is not longer maintained by the developers, but we still would like to use this module. The problem is, the code is not compatible with ...
2
votes
0answers
34 views

Magento 2: Best practice create model class

First thing, I have a question on this topic Optimize load model memory Then I have some questions for the best way to create a Model to load or save data to Database? Could you please explain which ...
3
votes
0answers
375 views

Magento 2 Optimize memory leak perfomance

I have a problem with the import data process. First: I have used the factory creator to create a Model set data and save but the problem when I have 400k Item to save. --- Constructor \Magento\...
1
vote
1answer
215 views

Only dependency assignment operations are allowed in constructor. No other operations are allowed

MEQP2: How to resolve below issue? abstract class ClassName extends \Magento\Backend\App\Action { public function __construct( ... \Magento\Catalog\Helper\Product $productHelper, ...
2
votes
1answer
1k views

Module isn't overriding default Mail Transport in Magento 2.2

I started developing Magento 2.2 modules about 3 months ago. I did a very simple SMTP module by following what some open source projects did. I could use a free plugin but the real goal here is to ...
1
vote
0answers
19 views

Class instatiation with data

I have created custom Factory class for my custom model class. Of course basically you can instantiate it by calling $this->objectManager->create(Someobject::class);. But, ObjectManage has also ...
2
votes
1answer
28 views

How does M2 know what to inject based on our _construct?

public function __construct( \Magento\Catalog\Helper\Category $categoryHelper, .... Let's say we have code like that. How does Magento know to inject category helper and lets us use it? It ...
0
votes
4answers
663 views

Is there any way to identify what class causes 'Area code is not set' or 'Area code already set'?

I know there are a lot of questions about 'Area code is not set', but no one has yet answered the main question. How to find the file which caused this Exception? In my case, I updated a lot of ...
2
votes
2answers
1k views

Dependency injection confusion Magento 2

I hope this is the correct place to ask the question for this. I read the tutorial about DI for Magento 2, I almost get it the concept. How Magento 2 use di in the framework. After reading I get to ...
3
votes
1answer
559 views

Does arguments order in constructor matters?

I've noticed strange situation while creating class contructor (__construct). Here is my code: public function __construct( \Magento\Framework\Model\Context $context, \Magento\...
1
vote
2answers
922 views

What preference to use for \Magento\Framework\DB\Adapter\AdapterInterface?

I'm trying to override a Collection constructor and getting Fatal error: Uncaught Error: Cannot instantiate interface Magento\Framework\DB\Adapter\AdapterInterface in /var/www/magento/vendor/magento/...
0
votes
2answers
30 views

Inject class in custom Field

I created a backend configuration for my custom plugin /app/code/Sostanza/LiveHelp/etc/adminhtml/system.xml https://pastebin.com/2jwnYLwb /app/code/Sostanza/LiveHelp/Block/Button.php https://...