Magento 2 Development Best Practices

32
@benmarks ZendCon 2016 Magento 2 Development Best Practices

Transcript of Magento 2 Development Best Practices

Page 1: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Magento 2Development Best Practices

Page 2: Magento 2 Development Best Practices

@benmarks ZendCon 2016

[email protected]

@benmarks

Page 3: Magento 2 Development Best Practices

@benmarks ZendCon 2016

What’s your first M?

Page 4: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Have Proper Expectations• M1 != M2

• M1 expertise is helpful, but not imperative

• Framework + Digital Commerce Application

• Open Source

Page 5: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Know the Basics• Own MVC framework, own ORM

• PHP + XML + PHTML

• Composer

• PSR 1-4 (2*)

• Tests (unit, integration, functional)

• Native FPC. Many caches & indexes.

Page 6: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Have the right setup• Local environment, or…

• …Virtualization: Docker* & Vagrant

• Use an IDE. PhpStorm is great.

• Step debugger

• Documentation: devdocs.magento.com

• PHP 7!

Page 7: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Install the Right Way• Archive or Composer (installs under ./vendor) composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition

• Contributing (installs under ./app/code) git clone github.com/magento/magento2 (PRs: -b developer - not stable!)

• Sample data bin/magento sampledata:deploy bin/magento setup:upgrade

Page 8: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Prepare to develop• Enable Developer Mode bin/magento deploy:mode:show bin/magento d:m:set developer

• Disable onlbin/magento cache:disable config bin/magento cache:flush config

Page 9: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Understand Modes• Developer mode

• Static file materialization is not enabled.

• Uncaught exceptions displayed in the browser

• Exceptions thrown in error handler, not logged

• System logging in var/report, highly detailed.

• Production mode• Deployment phase on the production system;

highest performance

• Exceptions are not displayed to the user -- written to logs only.

• This mode disables static file materialization.

• The Magento docroot can have read-only permissions.

Default Mode: just no.

Page 10: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Know your Job Scope• Customizations may involve:

• New functionality

• Changed functionality

• Thematic changes

Page 11: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Register Wisely• Framework convention for registering

components: register.php & composer.json

• Modules • Themes • Libraries • Langage Packs

Page 12: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Use Others’ Work!• We went to the trouble to make M2 work with Composer

• PSR 1, 2, 3, 4

• Leverage the effort of others!

• marketplace.magento.com

• https://packagist.org/search/?q=magento%202

Page 13: Magento 2 Development Best Practices

@benmarks ZendCon 2016

https://github.com/SnowdogApps/magento2-theme-blank-sass

Page 14: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Follow coding standards

• Code demarcation standard

• PHP coding standard

• JavaScript coding standard

• jQuery widget coding standard

• DocBlock standard

• JavaScript DocBlock standard

• LESS coding standard

• HTML style guide

devdocs.magento.com/guides/v2.1/coding-standards/bk-coding-standards.html

Page 15: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Page 16: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Decouple!• Upgradeability and testability are essential

• Use composition (constructor DI)

• SPI - extension points (events, interfaces)

• Core can be a good reference

• @api - Use, don’t extend

Page 17: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Use D.I.

Page 18: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Use Interceptionbefore, after, around Plugins

Page 19: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Use Events

Page 20: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Page 21: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Be Aware of Generation• Depending on technique, and MAGE_MODE, class

definitions are generated from DI settings + definition

• Developer: normal step debugging

• Production: check ./var/generation for generated classes

Page 22: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Use UI Components• DRY system for data templating

• “Easy to use”

• Define data source, and go!

• Example

Page 23: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Use/extend CLI tool• bin/magento - extendable, one-stop shop for

developer functions

Page 24: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Test!

https://www.flickr.com/photos/akrabat/8421560178/

Page 25: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Page 26: Magento 2 Development Best Practices

@benmarks ZendCon 2016

OWASP!

Page 27: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Page 28: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Use Your Friends• Alan. Storm! - @alanstorm - alanstorm.com

• Alan Kent - @akent99 - alankent.me

• Magento SE - magento.stackexchange.com

• DevDocs - devdocs.magento.com

• Marketplace - marketplace.magento.com

Page 29: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Page 30: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Resources• PhpStorm + XDebug + Virtualization

• Docker: MageInferno hub.docker.com/u/mageinferno/

• Vagrant: github.com/paliarush/magento2-vagrant-for-developers

• Laravel Valet

• n98-magerun

• Dev toolbars:

• Inviqa: github.com/inviqa/MX_HelperBar

• Alan Storm’s Commerce Bug:store.pulsestorm.net/products/commerce-bug-3

Page 31: Magento 2 Development Best Practices

@benmarks ZendCon 2016

Page 32: Magento 2 Development Best Practices

@benmarks ZendCon 2016

[email protected]

@benmarks