27
@benmarks ZendCon 2016 Intro to Magento 2 Your First Module

Your First Magento 2 Module

Embed Size (px)

Citation preview

Page 1: Your First Magento 2 Module

@benmarks ZendCon 2016

Intro to Magento 2Your First Module

Page 2: Your First Magento 2 Module

@benmarks ZendCon 2016

[email protected]

@benmarks

Page 3: Your First Magento 2 Module

@benmarks ZendCon 2016

About Ben

• 2008-14: Developer, Director @ Blue Acorn

• 2011-15: Trainer @ Magento U

• 2014-now: Magento Evangelist @ Magento

Page 4: Your First Magento 2 Module

@benmarks ZendCon 2016

About you?

Page 5: Your First Magento 2 Module

@benmarks ZendCon 2016

M2 in General• Open Source, including the licensed Enterprise Edition

• Own PHP MVC Framework + Commerce Application

• Custom ORM & Service Layer

• Composer; PSR-1, -2*, -3, -4 (*style exceptions, to be resolved later)

• Two version types: “marketing” (e.g. 2.1) and “platform” (101.x.x)

• Heavily cached & indexed, with native FPC (Varnish)

• Testing framework, core test coverage (U, I, & F)

Page 6: Your First Magento 2 Module

@benmarks ZendCon 2016

Installing M2• 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 7: Your First Magento 2 Module

@benmarks ZendCon 2016

Development Prep• IDE with step debugger

Suggested: PhpStorm + xdebug (+ Magicento)

• Virtual Machines Community Vagrant & Docker options (see Resources slide at end) Official Docker image coming soon!

• Enable Developer Mode bin/magento deploy:mode:show bin/magento d:m:set developer bin/magento cache:disable config bin/magento cache:flush config

Page 8: Your First Magento 2 Module

@benmarks ZendCon 2016

A note on D.I. in M2 (1/2)• Dependencies should be provided via framework-managed

constructor injection

In M2 it is a best practice to write decoupled code.

Page 9: Your First Magento 2 Module

@benmarks ZendCon 2016

A note on D.I. in M2 (2/2)• M2 ships with an XML-configured dependency injection system

This system also serves as a customization platform for plugins and method arguments.

In M2 it is a best practice to write decoupled code.

Page 10: Your First Magento 2 Module

@benmarks ZendCon 2016

Module time!

Page 11: Your First Magento 2 Module

@benmarks ZendCon 2016

M2 Module StructurePresentation logicRequest handling

Configuration

Rendering

Exposes to app

Page 12: Your First Magento 2 Module

@benmarks ZendCon 2016

Module Registrationapp/code/BenMarks/Brands/ etc/module.xml registration.php

Page 13: Your First Magento 2 Module

@benmarks ZendCon 2016

Module Registration• Disable caches bin/magento cache:disable config full_page

• Verify changes are picked upbin/magento module:status

• Enable module bin/magento module:enable BenMarks_Brands

Page 14: Your First Magento 2 Module

@benmarks ZendCon 2016

Configure a Frontend Routeetc/frontend/routes.xml

http://site.com/brands/

uniqu

e iden

tifier

BenMarks/Brands/Controller/

Page 15: Your First Magento 2 Module

@benmarks ZendCon 2016

Controller Time (1/3)BenMarks/Brands/Controller/Index/Index.php

Page 16: Your First Magento 2 Module

@benmarks ZendCon 2016

Controller Time (2/3)• Test your route!

Go to e.g. http://site.com/brands/ …Why is the page blank?

• Time to add layout XML!Create e.g. view/frontend/layout/benmarks_brands_index_index.xml

• Disable/flush layout cache & refreshbin/magento cache:disable layout # or bin/magento cache:flush layout

Page 17: Your First Magento 2 Module

@benmarks ZendCon 2016

Controller Time (3/3)• Add a page title

Page 18: Your First Magento 2 Module

@benmarks ZendCon 2016

ORM (1/3): ModelBenMarks/Brands/Model/Brand.php

Page 19: Your First Magento 2 Module

@benmarks ZendCon 2016

ORM (2/3): Resource ModelBenMarks/Brands/Model/ResourceModel/Brand.php

Page 20: Your First Magento 2 Module

@benmarks ZendCon 2016

ORM (3/3): CollectionBenMarks/Brands/Model/ResourceModel/Brand/Collection.php

Page 21: Your First Magento 2 Module

@benmarks ZendCon 2016

Setup Script (1/3)BenMarks/Brands/Setup/InstallSchema.php

Page 22: Your First Magento 2 Module

@benmarks ZendCon 2016

Setup Script (2/3)• According to Magento, our module is already installed!

Need to remove module registration

• bin/magento setup:upgrade

Page 23: Your First Magento 2 Module

@benmarks ZendCon 2016

Setup Script (3/3)• Check that upgrade executed

Need to remove module registration

• Troubleshooting - check the setup_module table schema_version should match data_version

Page 24: Your First Magento 2 Module

@benmarks ZendCon 2016

Rendering a List1. Create a block

2. Add block reference to layout XML

3. Refresh!

Page 25: Your First Magento 2 Module

@benmarks ZendCon 2016

Resources (1/2)• AlanStorm.com

• pestle (CLI tool)http://alanstorm.com/magento2_pestle_code_generation/

• CommerceBug (commercial)http://store.pulsestorm.net/products/commerce-bug-3

• Mage2gen - https://mage2gen.com/

Page 26: Your First Magento 2 Module

@benmarks ZendCon 2016

Resources (2/2)• AlanKent.me

• VinaiKopp.com (Also https://www.youtube.com/c/mage2katas)

• DevDocs.Magento.com

• VMs:

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

• https://hub.docker.com/u/mageinferno/

Page 27: Your First Magento 2 Module

@benmarks ZendCon 2016

[email protected]

@benmarks