@benmarks ZendCon 2016
Intro to Magento 2
Your First Module
@benmarks ZendCon 2016
ben@magento.com


@benmarks
@benmarks ZendCon 2016
About Ben
• 2008-14: Developer, Director @ Blue Acorn
• 2011-15: Trainer @ Magento U
• 2014-now: Magento Evangelist @ Magento
@benmarks ZendCon 2016
About you?
@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)
@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
@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
@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.
@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.
@benmarks ZendCon 2016
Module time!
@benmarks ZendCon 2016
M2 Module Structure
Presentation logic


Request handling
Configuration
Rendering
Exposes to app
@benmarks ZendCon 2016
Module Registration
app/code/BenMarks/Brands/



etc/module.xml









registration.php
@benmarks ZendCon 2016
Module Registration
• Disable caches

bin/magento cache:disable config full_page
• Verify changes are picked up

bin/magento module:status





• Enable module

bin/magento module:enable BenMarks_Brands
@benmarks ZendCon 2016
Configure a Frontend Route
etc/frontend/routes.xml
http://site.com/brands/
unique
identifier
BenMarks/Brands/Controller/
@benmarks ZendCon 2016
Controller Time (1/3)
BenMarks/Brands/Controller/Index/Index.php
@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 & refresh

bin/magento cache:disable layout

# or bin/magento cache:flush layout
@benmarks ZendCon 2016
Controller Time (3/3)
• Add a page title

@benmarks ZendCon 2016
ORM (1/3): Model
BenMarks/Brands/Model/Brand.php
@benmarks ZendCon 2016
ORM (2/3): Resource Model
BenMarks/Brands/Model/ResourceModel/Brand.php
@benmarks ZendCon 2016
ORM (3/3): Collection
BenMarks/Brands/Model/ResourceModel/Brand/Collection.php
@benmarks ZendCon 2016
Setup Script (1/3)
BenMarks/Brands/Setup/InstallSchema.php
@benmarks ZendCon 2016
Setup Script (2/3)
• According to Magento, our module is already installed!

Need to remove module registration















• bin/magento setup:upgrade
@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
@benmarks ZendCon 2016
Rendering a List
1. Create a block
2. Add block reference to layout XML
3. Refresh!
@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/
@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/
@benmarks ZendCon 2016
ben@magento.com


@benmarks

Your First Magento 2 Module

  • 1.
    @benmarks ZendCon 2016 Introto Magento 2 Your First Module
  • 2.
  • 3.
    @benmarks ZendCon 2016 AboutBen • 2008-14: Developer, Director @ Blue Acorn • 2011-15: Trainer @ Magento U • 2014-now: Magento Evangelist @ Magento
  • 4.
  • 5.
    @benmarks ZendCon 2016 M2in 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)
  • 6.
    @benmarks ZendCon 2016 InstallingM2 • 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
  • 7.
    @benmarks ZendCon 2016 DevelopmentPrep • 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
  • 8.
    @benmarks ZendCon 2016 Anote 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.
  • 9.
    @benmarks ZendCon 2016 Anote 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.
  • 10.
  • 11.
    @benmarks ZendCon 2016 M2Module Structure Presentation logic 
 Request handling Configuration Rendering Exposes to app
  • 12.
    @benmarks ZendCon 2016 ModuleRegistration app/code/BenMarks/Brands/
 
 etc/module.xml
 
 
 
 
 registration.php
  • 13.
    @benmarks ZendCon 2016 ModuleRegistration • Disable caches
 bin/magento cache:disable config full_page • Verify changes are picked up
 bin/magento module:status
 
 
 • Enable module
 bin/magento module:enable BenMarks_Brands
  • 14.
    @benmarks ZendCon 2016 Configurea Frontend Route etc/frontend/routes.xml http://site.com/brands/ unique identifier BenMarks/Brands/Controller/
  • 15.
    @benmarks ZendCon 2016 ControllerTime (1/3) BenMarks/Brands/Controller/Index/Index.php
  • 16.
    @benmarks ZendCon 2016 ControllerTime (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 & refresh
 bin/magento cache:disable layout
 # or bin/magento cache:flush layout
  • 17.
    @benmarks ZendCon 2016 ControllerTime (3/3) • Add a page title

  • 18.
    @benmarks ZendCon 2016 ORM(1/3): Model BenMarks/Brands/Model/Brand.php
  • 19.
    @benmarks ZendCon 2016 ORM(2/3): Resource Model BenMarks/Brands/Model/ResourceModel/Brand.php
  • 20.
    @benmarks ZendCon 2016 ORM(3/3): Collection BenMarks/Brands/Model/ResourceModel/Brand/Collection.php
  • 21.
    @benmarks ZendCon 2016 SetupScript (1/3) BenMarks/Brands/Setup/InstallSchema.php
  • 22.
    @benmarks ZendCon 2016 SetupScript (2/3) • According to Magento, our module is already installed!
 Need to remove module registration
 
 
 
 
 
 
 
 • bin/magento setup:upgrade
  • 23.
    @benmarks ZendCon 2016 SetupScript (3/3) • Check that upgrade executed
 Need to remove module registration
 
 
 
 
 • Troubleshooting - check the setup_module table
 schema_version should match data_version
  • 24.
    @benmarks ZendCon 2016 Renderinga List 1. Create a block 2. Add block reference to layout XML 3. Refresh!
  • 25.
    @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/
  • 26.
    @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/
  • 27.