Magento 2 Modules are Easy!

30
Magento 2 Modules Are Easy. #magento2 @benmarks #ssphp16 [email protected]

Transcript of Magento 2 Modules are Easy!

Page 1: Magento 2 Modules are Easy!

Magento 2 Modules Are Easy.#magento2 • @benmarks • #ssphp16

[email protected]

Page 2: Magento 2 Modules are Easy!

Rome wasn’t built in a day

And neither was Magento 2.

Page 3: Magento 2 Modules are Easy!

The story of Magento 2 begins with Magento 1,which is now an eight year old technology

(March 31, 2008).

Page 4: Magento 2 Modules are Easy!

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).

Page 5: Magento 2 Modules are Easy!

Of course, building the most popular and unconventional open source commerce app taught us

some lessons.

Page 6: Magento 2 Modules are Easy!

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

Page 7: Magento 2 Modules are Easy!

Magento 1 Challenges…documentation?!?

Page 8: Magento 2 Modules are Easy!

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

Page 9: Magento 2 Modules are Easy!

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…

Page 10: Magento 2 Modules are Easy!

Magento 2 is the beginning of a new era, rather than an endpoint.

Page 11: Magento 2 Modules are Easy!

• Installation methods

• IDE + debugger

• Deployment modes

• Check CLI tool: $ bin/magento deploy:mode:set developer $ bin/magento dev:tests:run --help

Developer Preparations

Page 12: Magento 2 Modules are Easy!

• 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

Page 13: Magento 2 Modules are Easy!

Magento 2 Module Structure• ./vendor or ./app/code

• Basic module:

★ registration.php

★ etc/module.xml

Page 14: Magento 2 Modules are Easy!

registration.php

Page 15: Magento 2 Modules are Easy!

module.xml

Page 16: Magento 2 Modules are Easy!

Configuration• Configuration is split out by type

• Magento 2 provides XSD for config files; this helps IDEs & tooling!

• No more class groups

Page 17: Magento 2 Modules are Easy!

• In Magento 2, all dependencies should be constructor-injected

Dependencies & DI

Page 18: Magento 2 Modules are Easy!

• Dependency injection parameters are found in module di.xml files

DI Configuration

Page 19: Magento 2 Modules are Easy!

• Dependency injection results in synthesized class definitions in ./var/generation

DI generation

Page 20: Magento 2 Modules are Easy!

• 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

Page 21: Magento 2 Modules are Easy!

• 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

Page 22: Magento 2 Modules are Easy!

• 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

Page 23: Magento 2 Modules are Easy!

Setup Scripts• No more per-step files

Page 24: Magento 2 Modules are Easy!

Customization• Plugins: before, after, around (via DI)

• Preferences (via DI) ≈ rewrites in M1

• Events still there

Page 25: Magento 2 Modules are Easy!

• Execute before, after, around public methods

• Preferred method of customization

Plugins

Page 26: Magento 2 Modules are Easy!

• Swap classes

• Change arguments

Preferences & Params

Page 27: Magento 2 Modules are Easy!

Let’s make a module!

Page 28: Magento 2 Modules are Easy!

• github.com/magento/magento2 (and ./*)

• devdocs.magento.com

• community.magento.com

• magento.stackexchange.com

• alankent.me

• alanstorm.com

Resources

Page 29: Magento 2 Modules are Easy!

• 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

Page 30: Magento 2 Modules are Easy!

Thank you!

📧 [email protected]🐦 @benmarks

Feedback: http://bit.ly/m2-tut-1