Skip to main content
New
Stack Overflow Jobs powered by Indeed: A job site that puts thousands of tech jobs at your fingertips (U.S. only). Search jobs
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
105 votes
Accepted

When Should We Use a Repository and Factory in Magento 2?

If there is a repository and it does what you need well, always prefer the repository. Repositories are part of the Service Contracts (they are implementations of interfaces in Api), this means they ...
Fabian Schmengler's user avatar
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
47 votes
Accepted

Magento 2 as a headless solution

Answers to the questions Who is responsible for formatting data, for example prices. Magento API and frontend framework? Magento API provides access to the data and business logic. Formatting data/...
Sinisa Nedeljkovic's user avatar
29 votes

When Should We Use a Repository and Factory in Magento 2?

Good question. Even if both Repositories and Factories let us access an Entity I think we should focus on their responsibility. From Magento documentation: "Factories are service classes that ...
Alessandro Ronchi's user avatar
26 votes

Magento 2 - Database transaction for multiple model / resource model objects?

You are probably saving an aggregate of multiple connected objects. Determine which of these objects is the root, for example: [order] <------ this is the root ...
Fabian Schmengler's user avatar
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
24 votes

How to render HTML with AJAX in Magento 2

I would also go the way 2 and, indeed, you actually can render "pure" HTML via AJAX without the head, body, css and so on. The trick is to: tell your controller to instanciate a Response that is of ...
Hervé Guétin's user avatar
21 votes
Accepted

Magento 2: proper use of helpers

Don't. This is like using ObjectManager::getInstance()->create() in a template! Use a custom Block that receives the helper as a constructor dependency instead, and add a proxy method that calls ...
Vinai's user avatar
  • 14k
21 votes
Accepted

Magento 2 - good practice to use/avoid magic getters?

The above question is about using magic methods vs. getDataByKey or getDataByPath. I think there is a third option, too, and that is implementing real getter and setter methods. The getData* methods ...
Vinai's user avatar
  • 14k
20 votes
Accepted

Best Way to Load a Custom Model in Magento 2

Best practice: via the service contract The best practice is always to use the service contract whenever it's possible. You can find the list of reasons here: Magento 2: what are the benefits of ...
Raphael at Digital Pianism's user avatar
19 votes

How to write a custom extension?

If you sell your extension or share it with others, think about writing code that is human readable. don't make method too complex add DOC blocks to your methods * use proper variable names, like $...
sv3n's user avatar
  • 11.7k
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
16 votes
Accepted

Magento 2 - How to read the values in etc/config.xml?

You cannot read the values directly from etc/config.xml. I mean, you can, but there is no point in reading directly from there since the values may be overwritten from the stores->configuration ...
Marius's user avatar
  • 198k
15 votes
Accepted

How to manage config.php in Magento 2

First of all, very good question. But...I don't agree with this: The problem is when someone install the project he doesn't have this file but he already have the database so he can't run bin/...
Marius's user avatar
  • 198k
15 votes
Accepted

Why use a theme when you have modules?

You can do anything you fancy, but if it is the right thing to do that is the question here. Modules Modules should add or change functionality of the webshop and should/could be transferred to ...
7ochem's user avatar
  • 7,542
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
13 votes

Magento 2 as a headless solution

I can share some insights of developing headless magento projects as my company has created 2 of them. We have decided to use reactjs as a frontend application and connect it with node powered ...
Zefiryn's user avatar
  • 5,898
12 votes
Accepted

Magento 2: what's the status of M2 regarding PSR compliance?

PSR-3 is already used (with Monolog as default implementation) PSR-4 is already used (via composer) PSR-6 is not supported and I know of no plans to do so, but there is a bridge implementation: ...
Fabian Schmengler's user avatar
11 votes

What are the best practices for installing third party extensions in Magento 2?

Install module via composer is the best way to Magento 2. Becuase there are many advantages if we install module via composer. If you want to upgrade module you just need to change the version in ...
Prince Patel's user avatar
  • 22.7k
11 votes
Accepted

Magento 1: layout action tag best practice

So this is basically only allow two different ways to do the same thing. The code that actually picks which one to use has the "fallback". It can be found in Mage_Core_Model_Layout::_generateAction ...
David Manners's user avatar
10 votes

Magento 2 best practice for class locations and names

Magento 2 is not restricted as Magento 1 to only a few folders like block, helper, model and so on. You can basically place a class in any folder you want. It's up to you since the class are ...
Marius's user avatar
  • 198k
9 votes

How to write a custom extension?

@marius, regarding Coding Standards (point 24 in your list). I like to use PHP_CodeSniffer along with EQP and ECG CS to automatically enforce these standards. Using PHP_CodeSniffer you don't have ...
diazwatson's user avatar
  • 2,430
9 votes
Accepted

Magento 2 Workflow and Development Tools

Integer_Net made a great article about Magento 2 frontend workflow, I suggest you check it out: https://www.integer-net.com/magento-2-frontend-workflow-step-by-step/ Basically the idea is: setup ...
Raphael at Digital Pianism's user avatar
9 votes

Magento 2 best practice for class locations and names

I think the top priority should be to make the code as self documenting as possible. So rather than putting everything into Model or Helper directories, finding a good name that describes what the ...
Vinai's user avatar
  • 14k
9 votes
Accepted

Magento best practices: try / catch in templates

In my opinion templates don't have to contain any logic on that point. If we take your exemple, the best way is to make the try / catch in the block, the template is here only to display data. Block ...
Matthéo Geoffray's user avatar
8 votes

Dependency Injection and constructor parameters

Short answer: Your extension will crash. Long answer: If they change a constructor signature, the semantic version (major version) of the core module will be increased. That's why you should declare ...
Marius's user avatar
  • 198k
7 votes

Magento 2: how to use the collection walk iterator?

Yes, it's possible! It's very similar to Magento 1, by the way. \Magento\Framework\Model\ResourceModel\Iterator has the walk function which will call your callback function for every single item, ...
Matheus Gontijo's user avatar
7 votes
Accepted

Most efficient way to get data in templates

Before I give my answer I believe you may have a problem in your code - sessions should always be called as a singleton. Thus, the line Mage::getModel('checkout/session') should be Mage::getSingleton('...
philwinkle's user avatar
  • 35.8k

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