Magento 2 Modules are Easy!
Transcript of Magento 2 Modules are Easy!
Magento 2 Modules Are Easy.#magento2 • @benmarks • #ssphp16
Rome wasn’t built in a day
And neither was Magento 2.
The story of Magento 2 begins with Magento 1,which is now an eight year old technology
(March 31, 2008).
When Magento 1.0 released, PHP was at version 5.2, MySQL was at version 5.0, and no one was
testing PHP apps (but PhpUnit was at version 3.2).
Of course, building the most popular and unconventional open source commerce app taught us
some lessons.
Magento 1 Challenges• Confusing configuration implementation
• Non-unified module structure
• Internal coupling
• Extensible, but constrained by form & function
• Difficult to test
Source: http://www.osnews.com/story/19266/WTFs_m
Magento 1 Challenges…documentation?!?
Modernizing Magento for 2016+• PHP 7 support
• GitHub-based collaboration
• Composer-based DM
• Dependency Injection
• PSR adoption (1, 2, 3, 4)
• Updated frontend tech
• Extensible CLI tool
• Test coverage & framework
Modernizing Magento for 2016+• New checkout flow & architecture
• Performance = CE, Scale = EE
• EE licensing independent of server architecture
• Quarterly feature releases
• Proposed EE roadmap for 2016:
✦ ElasticSearch, B2B suite, Staging & Preview, Enhanced CMS functionality
✦ More…
Magento 2 is the beginning of a new era, rather than an endpoint.
• Installation methods
• IDE + debugger
• Deployment modes
• Check CLI tool: $ bin/magento deploy:mode:set developer $ bin/magento dev:tests:run --help
Developer Preparations
• Module structure is quite different
• Configuration is slightly different
• Controllers are slightly different
• View is quite different
• ORM is mostly the same; yes EAV still is used, and no, MongoDB is not the solution
• Install/Upgrade (setup) scripts have changed
• New customization options (plugins & preferences)
Framework Notes M1 <> M2
Magento 2 Module Structure• ./vendor or ./app/code
• Basic module:
★ registration.php
★ etc/module.xml
registration.php
module.xml
Configuration• Configuration is split out by type
• Magento 2 provides XSD for config files; this helps IDEs & tooling!
• No more class groups
• In Magento 2, all dependencies should be constructor-injected
Dependencies & DI
• Dependency injection parameters are found in module di.xml files
DI Configuration
• Dependency injection results in synthesized class definitions in ./var/generation
DI generation
• Routes are declared in module route.xml files
• Route handlers are instance of \Magento\Framework\App\Action\Action
• One class per action, one action per class (different from M1 controllers)
• Action->execute()
Controllers
• Instance of \Magento\Framework\View\Element\AbstractBlock
• Templates simplified, most logic moved to block classes
• Layouts broken out by handle, i.e. no longer ~1 per module
• Templates & layouts moved inside module’s directory
• jQuery, RequireJS, JS Mixins, LESS
Views
• Model, resource model, collections still there
• No configuration for types
• Simple, complex, EAV
• API (SOAP & REST) are still available
• Service contracts should be on your radar
ORM
Setup Scripts• No more per-step files
Customization• Plugins: before, after, around (via DI)
• Preferences (via DI) ≈ rewrites in M1
• Events still there
• Execute before, after, around public methods
• Preferred method of customization
Plugins
• Swap classes
• Change arguments
Preferences & Params
Let’s make a module!
• github.com/magento/magento2 (and ./*)
• devdocs.magento.com
• community.magento.com
• magento.stackexchange.com
• alankent.me
• alanstorm.com
Resources
• Several classes available online
• Fundamentals class available online with in-person component
• M2-based certifications work begins later in 2016
• I have discount vouchers for you!
Magento U