Questions tagged [best-practice]
Denotes questions related to best-practices in Magento.
208
questions
160
votes
10
answers
183k
views
To use or not to use the ObjectManager directly?
Ok, so yesterday we had a big talk with other people from the Magento community regarding the direct use of the ObjectManager in classes/templates.
I'm already aware of the reasons why we shouldn't ...
148
votes
5
answers
12k
views
How to write a custom extension?
Because I had a lot of issues lately with free and commercial extension I've decided to ask this question and answer it with the steps I usually follow when writing an extension. Feel free to edit the ...
108
votes
6
answers
45k
views
When Should We Use a Repository and Factory in Magento 2?
I have gone through a couple of tutorials in Magento 2, and this confuses me a little. I can see there are basically two ways by which we can read/write business entities:
Retrieve Data
Using A ...
55
votes
3
answers
18k
views
Magento 2 as a headless solution
I want to know if there are some best practices to use Magento 2 as a headless E-commerce solution.
A typical E-commerce in 2017 is to have an omni-channel solution which includes
E-commerce
CMS
...
53
votes
6
answers
17k
views
Modern Magento 1.X Workflow and Development Tools
I am new to Magento Development ( CE 1.6 ) and am still trying to define my workflow. I currently develop on Mac OSX 10.8 with Netbeans 7.3, but I find that Netbeans is slow and freezes on me. I tend ...
45
votes
2
answers
22k
views
What's the preferred way of throwing exceptions in Magento?
All of the following methods are used in Magento core so which one is the preferred (or the latest "best practice") way?
Mage::throwException('Some Message') - 732 Usages
throw new Exception('Some ...
27
votes
4
answers
34k
views
Efficient get product url from id
What is the most efficient way of getting a product url given just it's ID? In a few places in our code we have things such as Mage::getModel('catalog/product')->load($id)->getProductUrl() in ...
27
votes
4
answers
4k
views
Returning $this after observer
I see some conflicting information on the internet and in 3rd party modules alike - is it a requirement or best practice to return $this at the end of an observer method?
E.g.:
...
22
votes
6
answers
18k
views
What is helper in Magento?
What is helper in Magento?
In what cases should one use and not use helpers?
21
votes
2
answers
3k
views
Magento 2 - good practice to use/avoid magic getters?
Magic getters on Varien_Object (M1) and DataObject (M2) are common practice, but with Magento 2 it feels wrong to use it.
Good:
easy to read/write
Bad
It causes problems when using digits in keys (...
19
votes
2
answers
20k
views
Best Way to Load a Custom Model in Magento 2
Because it was hard for me to find the right way, below you could find the best practice I made mine. Enjoy, correct my English if needed and say me I'm wrong if I am. :)
Edit : ... and I found out I ...
19
votes
2
answers
1k
views
Magento 2 DI best practices
Let's say I'm building a Magento 2 extension that does....well...not important. Let's say it does super awesome stuff.
But I want to make sure that this is build using the proper standards so other ...
17
votes
5
answers
5k
views
What are the best practices for installing third party extensions in Magento 2?
While working on a client project for Magento 2 - I've discovered numerous methods of loading in and keeping track of third party extensions.
Going into this assuming that we're using the integrator ...
17
votes
1
answer
4k
views
What is the best practice way in Magento 2 for creating Many To Many relationships?
I've looked around the core and seen a few examples of many to many relationships between models, but I can't see a definitive answer on this.
As an example, say we create a new model and we want to ...
16
votes
5
answers
41k
views
Magento2: Best practice to modify css files
I want to modify css file pub/static/frontend/Magento/luma/en_US/css/styles-l.css.
Initially this file is not present in pub/static folder and it's present in
vendor/magento/theme-frontend-blank/web/...
15
votes
3
answers
23k
views
Magento : Fastest way to update a product attribute
I'm looking for the fastest and reliable method for mass attribute update.
Below are the methods am aware of but I don't know with which I should go.
$store_id = 0;
Mage::getSingleton('catalog/...
15
votes
4
answers
3k
views
Magento 2 best practice for class locations and names
In Magento 1 we were used to place our classes in these directories
Block
Helper
Model
Resource
and use a simple class name without any capital letters in the middle of the name.
...
15
votes
1
answer
11k
views
Magento 2 - Database transaction for multiple model / resource model objects?
If I have a logic that involve updating multiple model objects which will update a few tables in database, how to provide a database transaction to ensure the data integrity?
15
votes
1
answer
8k
views
How to manage config.php in Magento 2
First of all it is maybe an already answered question but I did not find anything about that subject.
It is not an issue but a question about the best way to manage the file config.php in Magento ...
14
votes
4
answers
2k
views
Is it good to instantiate a getModel class on phtml templates?
This is a question regarding a good programming practices in Magento.
I need to show (in the category product list) the product with its related products in thumbnails. So I edited mypackage/mytheme/...
13
votes
1
answer
2k
views
Avoiding save in a loop in mass action
I've created my own CRUD module that contains an inline edit action similar to the one for CMS pages
Everything works OK, but when running phpsniffer with the EcgM2 standard I get this warning:
...
13
votes
1
answer
309
views
Is there a good reason to put the same code in both try AND catch blocks?
In the file: app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer/Datetime.php, within the render() method, it seems like the exact same code is being run in both the try and catch blocks. ...
12
votes
5
answers
22k
views
How to render HTML with AJAX in Magento 2
I try to find the best way to render HTML through AJAX in Magento 2.
Way 1 : Using Controller without layout
File Foo/Bar/Controller/Popin/Content.php
<?php
namespace Foo\Bar\Controller\...
11
votes
4
answers
25k
views
Best practice to join product attributes
I have a custom table with a product reference product_id. Now I would like to show product information (sku, name) in my backend grid, but I am unsure what is the best practice to do this?
My best ...
11
votes
3
answers
7k
views
Right way to implement getExtensionAttributes()
I was wondering, what's the right way to implement an extensible EAV model.
I see that in Magento\Catalog\Model\Product, the method getExtensionAttributes() is implemented like this:
public function ...
10
votes
3
answers
5k
views
Magento 2: proper use of helpers
I'm starting to see more and more people declaring helpers classes in order to be able to use the following in the template files:
$this->helper('Path/To/Helper/Class')->customMethod();
This ...
10
votes
3
answers
2k
views
ECG compliance: How to do?
There is something like a coding standard for Magento 1, called ECG where numerous things should be avoided or methods that should not be used.
Let's try to create a list that covers all cases that ...
10
votes
5
answers
2k
views
Where do I put .php, .js, .html, .css files from a 3rd party lib that interfaces with an extension I develop?
Let's say I want to develop a Magento extension that interfaces to, say, an Open Source charting package or an image gallery or whatever that is NOT part of the extension itself. When downloaded (...
10
votes
2
answers
802
views
How To Properly Disable a (Model) Rewrite
There is a third-party extension which rewrites a specific Magento core class. It should not matter if it is a block, helper or model, but in this case it is a model. Now I encounter that this rewrite ...
10
votes
3
answers
2k
views
Magento 1: Performance optimizations to delete entities
I'm currently trying to improve a couple of modules regarding performance.
Some of you may know the usage of the walk() method on collection which is very useful to avoid looping through products ...
9
votes
3
answers
17k
views
Best Practise to Add Custom Tab in Product Details (backend)
I would like to add an extra tab to the product details ( backend ) and I don't want to perform any override, if possible.
What is the best way to accomplish this ?
9
votes
2
answers
1k
views
Why use a theme when you have modules?
Inside of my custom module I have added the file
Vendor/Theme/view/layout/default.xml
which I am using to override a lot of the site's global templating with templates/blocks/layouts from the custom ...
9
votes
1
answer
12k
views
Magento 2 - How to read the values in etc/config.xml?
How to define and read the values in etc/config.xml in a custom module?
What is the best practice to use it?
9
votes
1
answer
907
views
Magento 2: what's the status of M2 regarding PSR compliance?
So according to the official documentation, I understand that Magento 2 is PSR-1 and PSR-2 compliant.
I'm wondering if Magento 2 is (or will be) compliant with the others ones ?
PSR-3: Logger ...
9
votes
3
answers
9k
views
Magento 2 - Best practice to get current date
I have to save an updated_at attribute of a custom entity and to know which is the best practice I took a look at how Magento 2 handles it.
I looked at the \Magento\Eav\Model\Entity\Attribute\Backend\...
8
votes
2
answers
3k
views
How to handle deprecated "Mysql4" classes when rewriting resource models
I have to rewrite Mage_Catalog_Model_Resource_Category_Flat.
The old resource class Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat is still there in Magento 1.7 for backwards compatibility and ...
7
votes
2
answers
635
views
Strategy for rewriting classes
[TL:DR]
I have Core_Class_A and Core_Class_B extends Core_Class_A.
I need to rewrite both of them and add a common functionality.
So I made this:
Custom_Class_A extends Core_Class_A.
The question ...
7
votes
3
answers
18k
views
Magento 2: How to get data from controller to block/view
I am looking for the right flow of data of how Magento wants me to do it. I have some data which I want to pass to the view. How do I do this the right way?
I saw the following answer for Magento 1.x ...
7
votes
6
answers
8k
views
Best practice for local development
I've been trying to find the best way on how to develop a Magento 2 webshop locally. So far, I had no luck: lots of JS, CSS and 404 errors (on which I tried lots of fixes which only partly solved my ...
7
votes
1
answer
3k
views
Magento 2 Workflow and Development Tools
I am curious what other members of the community are using and suggest for M2 development:
"What are some suggested Magento 2.X workflows?"
"What tools/configurations/plugins are being used for ...
7
votes
2
answers
94
views
Is it best practice for a Block function to call a ResourceModel function?
Is it appropriate for a Block class to retrieve data through a function in a ResourceModel class, rather than a Model class?
Or should the Block call the Model which then passes the call to the ...
7
votes
1
answer
535
views
Best practices for running and updating multiple separate stores
3 years ago I was a part of a company that ran magento webshops for many independent customers. What we experienced at the end was that if we wanted to do a change that affects all the shops (bug fix) ...
7
votes
3
answers
8k
views
Best Practice for staging/development site
I am new to magento and have been working a my first site and it is now ready to go live. I have been advised that once live to also build a staging or development site so that i can in the future ...
7
votes
1
answer
219
views
Good practice regarding core tables relations with modules tables
This a question regarding Magento development, it can apply for both M1 and M2.
For the last weeks, I've been working on several modules that are directly related to native Magento entities/database ...
7
votes
2
answers
298
views
How to avoid Law of Demeter violation ("Newable objects should not hold a field reference to an injectable object")
In the Rules for using dependency injection, the Magento 2 devdocs state:
Newable objects should not hold a field reference to an injectable object nor should they request one in their constructor. ...
7
votes
1
answer
1k
views
Attrbributes Sets & Attributes: Non-Technical Overview/Logic
I work on the 'business' side of Magento (i.e. I'm not a developer). I'm looking to get a better understanding of the theory behind Attributes & Attribute Sets.
My goal: Come up with a few rules ...
6
votes
4
answers
10k
views
Magento 2: how to use the collection walk iterator?
Back in Magento 1, it was possible to use the collection iterator to walk through the results and avoid looping through them.
It was a huge improvement in terms of performance when dealing with ...
6
votes
1
answer
872
views
Magento 1: layout action tag best practice
While studying for the Frontend Certification, I noticed a new thing I never came accross.
Basically when I want to use the <action> tag, let's say to change the template of a block, I'm used ...
6
votes
2
answers
483
views
Mage_Catalog_Model_Product_TYPE::TYPE_CONFIGURABLE vs 'configurable'
I'm working on an heavy customised magento version. Cancelling an order returned this fatal error:
Mage_Catalog_Model_Product_TYPE not found
on this line code:
if($parent->getTypeId() == ...
6
votes
1
answer
153
views
Usage of superfluous (and potentially dangerous) standard code in installer scripts
I always thought it's some kind of epidemic plague that users behave in a high risk manner introducing $installer->startSetup() and $installer->stopSetup() calls in installer scripts.
Many of ...