cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.x Best Practices and Conventions

Magento 2.x Best Practices and Conventions

Based on the first post related to best practices in Magento 1 (https://community.magento.com/t5/Share-Your-Best-Practices-Tips/Magento-1-x-Best-Practices-and-Conv...), I create this post to discuss about the best practices and conventions in Magento 2.

 

I will start by adding the best practices from Magento 1 that also apply to Magento 2. Later, I will add the new best practices that apply only to Magento 2, and I will also add your best practices to this post.

 

By doing that, we can use this first post as a "Wiki" for best practices and conventions for Magento 2.

 

 

Back-end

 

  1. Do not modify the Magento core: app/code/Magento
    core-2.png
    This is the #1 rule when it comes to customizing Magento. To be specific, the files from the app/code/Magento folder should never be modified to ensure the stability and upgradability of the Magento store.

  2. Avoid rewriting classes, use Magento 2 plugins instead

  3. If you rewrite a class, then annotate the changes you made to the core file

  4. Avoid implementing a lot of observers

  5. Remember setting magento in Developer Mode through the bin/magento command line tool

  6. Always turn off the cache in your local machine to avoid losing time

  7. Document your classes and methods using PHPDoc comments
    docs.png

  8. Limit the usage of PHP in the front-end

  9. Do not use direct SQL queries to connect to the database, use models instead

  10. Follow Magento naming conventions for classes, methods and files
    1. Methods and variable names should use camelCase
    2. Indexed arrays should use snake_case keys
    3. PHP variable names in the front-end should start with $_ (underscore after $ sign)

Front-end

 

  1. Don't use inline CSS in the front-end

  2. You can use tables, but only if you want to present tabular data

  3. Use image sprites to load icons in your theme
If my answer was useful, please give "Kudos" to this post.
If my answer helps you to resolve the problem, click on "Accept as Solution".