magento 2 development best practices
TRANSCRIPT
@benmarks ZendCon 2016
Magento 2Development Best Practices
@benmarks ZendCon 2016
What’s your first M?
@benmarks ZendCon 2016
Have Proper Expectations• M1 != M2
• M1 expertise is helpful, but not imperative
• Framework + Digital Commerce Application
• Open Source
@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.
@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!
@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
@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
@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.
@benmarks ZendCon 2016
Know your Job Scope• Customizations may involve:
• New functionality
• Changed functionality
• Thematic changes
@benmarks ZendCon 2016
Register Wisely• Framework convention for registering
components: register.php & composer.json
• Modules • Themes • Libraries • Langage Packs
@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
@benmarks ZendCon 2016
https://github.com/SnowdogApps/magento2-theme-blank-sass
@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
@benmarks ZendCon 2016
@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
@benmarks ZendCon 2016
Use D.I.
@benmarks ZendCon 2016
Use Interceptionbefore, after, around Plugins
@benmarks ZendCon 2016
Use Events
@benmarks ZendCon 2016
@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
@benmarks ZendCon 2016
Use UI Components• DRY system for data templating
• “Easy to use”
• Define data source, and go!
• Example
@benmarks ZendCon 2016
Use/extend CLI tool• bin/magento - extendable, one-stop shop for
developer functions
@benmarks ZendCon 2016
Test!
https://www.flickr.com/photos/akrabat/8421560178/
@benmarks ZendCon 2016
@benmarks ZendCon 2016
OWASP!
@benmarks ZendCon 2016
@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
@benmarks ZendCon 2016
@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
@benmarks ZendCon 2016