Skip to main content
118 votes
Accepted

To use or not to use the ObjectManager directly?

You should not use the ObjectManager directly! Exception from the rule are: in static magic methods like __wakeup, serialize, etc in case you should make backward compatibility of constructor in ...
KAndy's user avatar
  • 20.9k
63 votes

To use or not to use the ObjectManager directly?

So why does M2 sometimes access object manager directly when we recommend against it? Brutal answer: M2 is a port of M1 - not a complete rewrite. So don't assume that all the M2 code is perfectly ...
Alan Kent's user avatar
  • 3,498
60 votes

To use or not to use the ObjectManager directly?

You should never use \Magento\Framework\App\ObjectManager::getInstance(). It defeats the purpose of dependency injection. We're back at Mage::getModel(). Object manager should be used only in ...
Marius's user avatar
  • 198k
42 votes
Accepted

Magento2: Get Media URL In Template File (Without direct Calling Object Manager )

You can get media url in your template file using below way but without using objectmanager you must have to define Block file with __construct() method with define storeManagerInterface in construct ...
Rakesh Jesadiya's user avatar
40 votes

Fatal error: Cannot instantiate interface in Magento 2?

I think you have not mentioned the preference in your di.xml. I also faced the same issue when I was creating custom config xml file module. I got the issue when I called this code: $testConfig = $...
user49278's user avatar
  • 401
24 votes

To use or not to use the ObjectManager directly?

Why is Magento doing what they recommend us not to do ? Does that mean there are some cases where we should use the ObjectManager directly ? If so, what those cases ? Without knowing the full story ...
Kristof at Fooman's user avatar
23 votes
Accepted

Magento2: Load Model Data By Another Field

You need to use collections for that: $messages = $objectManager->create('Custom\Module\Model\ResourceModel\Messages\Collection')->addFieldToFilter('posts_id', $this->getRequest()->...
Raphael at Digital Pianism's user avatar
19 votes

Fatal error: Cannot instantiate interface in Magento 2?

I struggled with this and then found a solution that worked for me in the documentation. Be sure to add your interface as a preference (correctly) in the di.xml, and then run php bin/magento setup:di:...
Tristanisginger's user avatar
19 votes
Accepted

Magento 2.2.2 how to check if current page is a CMS page in .phtml file?

There are no registry variable add at cms page. So, if you want to check a page is CMS page then you need to check current controller and action name means Full action name Best practices In ...
Amit Bera's user avatar
  • 77.6k
17 votes

Magento2: Get Media URL In Template File (Without direct Calling Object Manager )

As of 2.1, there is no direct way of getting the media URL without either: calling the object manager directly (please don't do that) override the block and add a new method Rakesh mentioned one way ...
Raphael at Digital Pianism's user avatar
17 votes
Accepted

What is the proper way for unit testing PHP7 code with PHPUnit 4.1 in Magento 2?

Using the bundled PHPUnit version, even if it's ancient, is probably the best way to go since that will allow running the tests for all modules together during CI. I think writing tests in a manner ...
Vinai's user avatar
  • 14k
17 votes

How to load product collection in Magento 2

The standard collections are actually very similar. There are other similar structures, but for products you can load them in and load them just like Magento 1. Just like in Magento 1, if you are ...
Robert Egginton's user avatar
16 votes

To use or not to use the ObjectManager directly?

Should not use Object manager directly! For instance: \Magento\Framework\App\ObjectManager::getInstance(); also if you are working with event observers or plugins, you should never use it directly....
Ronak Chauhan's user avatar
15 votes
Accepted

How to create a productrepository using object manager in Magento 2?

No. Noooo. Not the right approach. You haven't given much information about exactly what problem you are trying to solve, but you should never be loading or saving any model directly in a template. ...
Ryan Hoerr's user avatar
  • 12.3k
14 votes

To use or not to use the ObjectManager directly?

The main reason that developers are strongly discouraged from using the Object Manager directly is that direct use of the Object Manager causes the extension not to be installable in compiled ...
Dewi Morgan's user avatar
14 votes

Magento2: Load Model Data By Another Field

You don't need to use collection for that. Just call the load function, with a second parameter, the field. Inject the factory in the construct where you need to retrieve your model protected $...
Pol Ravalitera's user avatar
13 votes
Accepted

Magento 2: What is Difference between Create & Get ObjectManager?

$_objectManager->create() creates new instance of the object, no-matter-what $_objectManager->get() first tries to find shared instance (already created), if it's not found - it just creates new ...
Sinisa Nedeljkovic's user avatar
12 votes
Accepted

Magento 2 : When to use Object Manager in Unit tests?

The short answer is, yes, I would mock the entire Context class. $mockScopeConfig = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); $mockContext = $this->getMock(\...
Vinai's user avatar
  • 14k
12 votes
Accepted

How to get ScopeConfigInterface through the object manager of unit tests in magento 2?

I may be wrong here, but I think for unit tests you don't have to retrieve values from the data base. You can assume that the implementations of \Magento\Framework\App\Config\ScopeConfigInterface are ...
Marius's user avatar
  • 198k
11 votes
Accepted

Magento2: Checking if a module exists, after being injected via class constructor

First Approach: The cleanest way is to not take decisions in the constructor. Let the di.xml do that for you. Let's say the core class you are using if a module is not present is Some\Core\ClassName. ...
Marius's user avatar
  • 198k
11 votes

Get order id by order increment id in Magento 2

You don't need to get the collection of order instead use \Magento\Sales\Model\Spi\OrderResourceInterface to get a single order object use Magento\Sales\Api\Data\OrderInterface; class Mageprince { ...
Prince Patel's user avatar
  • 22.8k
10 votes

Fatal error: Cannot instantiate interface in Magento 2?

You should not use the ObjectManager. Collect your dependencies via _constructor. In case your interface still won't be loaded, check your di.xml and validate that a preference is available.
David Lambauer's user avatar
10 votes
Accepted

How can we identify whether the class is injectable or not in Magento 2

Usually everything is injectable except models that have a table (or more) associated to it. For example the product model, the order model... These should not be injected in the constructor because ...
Marius's user avatar
  • 198k
9 votes

How to load product collection in Magento 2

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $...
megi's user avatar
  • 125
9 votes
Accepted

Get customer name from customer id using object manager

Try below code : $customerID = 10; // your customer-id $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerObj = $objectManager->create('Magento\Customer\Model\Customer')...
Manashvi Birla's user avatar
9 votes
Accepted

What is the use of pagefactory in Magento 2?

Pagefactory is used to initialize the layout. Its working in magento2 same like as in magento1 was doing with below code $this->_view->loadLayout(); $this->_view->renderLayout();
Ansar Husain's user avatar
  • 3,439
9 votes
Accepted

Get order id by order increment id in Magento 2

Try below Code $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $incrId = 100005363; $collection = $objectManager->create('Magento\Sales\Model\Order'); $orderInfo = $...
Sanjay Gohil's user avatar
  • 2,200
9 votes

Get order id by order increment id in Magento 2

I would not use any of the current answers as they rely on model’s load() method that has been deprecated since 2.1. Here are two ways you could do it using future-proof approaches: Use the order ...
fantasticrice's user avatar
8 votes

Magento2: Get Media URL In Template File (Without direct Calling Object Manager )

If you don't want to go to the trouble of extending \Magento\Framework\View\Element\Template, and you want your .phtml file to use the block \Magento\Framework\View\Element\Template, then you can use ...
kig's user avatar
  • 169
8 votes
Accepted

Can a virtual type be used as a dependency for a class?

What you're trying won't work because php must be able to find the class that you are giving in a constructor argument's type hint. This is part of the language and can't be overriden. Php is unaware ...
Aaron Allen's user avatar
  • 9,009

Only top scored, non community-wiki answers of a minimum length are eligible