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 ...
99
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 ...
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 ...
60
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 ...
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/...
30
votes
Magento : Fastest way to update a product attribute
Actually there are 3 ways to update an attribute on a product without save the full product.
Depending on the code/requirements one can be faster that the other.
Method 1:
$product = Mage::...
28
votes
Accepted
What is helper in Magento?
Theoretically you should never use helpers.
Helpers are just collections of unrelated methods and are always instantiated as a singletons.
This is basically procedural programming with functions ...
27
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 ...
25
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
...
24
votes
How to write a custom extension?
Andreas von Studnitz and Dr. Nikolai Krambrock gave a good presentation on code quality on the Meet Magento DE 2014. They distinguish between general code quality and Magento-specific code quality. In ...
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 ...
23
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 ...
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 ...
20
votes
Accepted
Magento2: Best practice to modify css files
You should not edit/modify files within pub/* or vendor/* directory. Pub is for deployment and vendor is for default structure, which you override via your template or custom modules. Instead:
create ...
20
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 ...
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 $...
19
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 ...
16
votes
Accepted
Best Practise to Add Custom Tab in Product Details (backend)
Personally I would go for a layout/action approach using the addTab() provided by Mage_Adminhtml_Block_Widget_Tabs
So 2 main action are involved here:
add XML layout changes
create the tab class
( ...
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....
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 ...
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 ...
14
votes
Magento 2: How to get data from controller to block/view
One approach is to use the registry so in your controller class you put it in the registry, and then in your block you can retrieve it.
<?php
namespace <Vendor>\<ModuleName>\...
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 ...
14
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/...
13
votes
What is helper in Magento?
The question has two aspects:
Should I write my own helpers and what for?
Should I use the core helpers and what for?
1. Writing helpers
In general, having classes named Helper, Util or similar ...
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 ...
12
votes
Accepted
Strategy for rewriting classes
After having a look at your problem with this very basic class inheritance diagram (sorry for the bad drawing, by the way), I say it's better to continue with the approach you have taken and create ...
11
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: ...
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 ...
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
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
best-practice × 201magento2 × 99
magento-1.9 × 31
magento-1 × 23
layout × 14
extensions × 14
module × 13
blocks × 13
magento-2.1 × 12
template × 11
database × 9
overrides × 9
coding-standards × 9
php × 8
theme × 8
dependency-injection × 8
event-observer × 7
collection × 7
performance × 7
product × 5
magento2.3 × 5
javascript × 5
model × 5
development × 5
workflow × 5