SlideShare a Scribd company logo
@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

More Related Content

PDF
Magento 2 Development Best Practices
PPTX
MidwestPHP - Getting Started with Magento 2
PDF
Magento 2 Modules are Easy!
PPT
12 Amazing Features of Magento 2
PDF
The journey of mastering Magento 2 for Magento 1 developers
PPTX
Madison PHP - Getting Started with Magento 2
PPTX
Magento 2: A technical overview
PPTX
[DevDay2019] Micro Frontends Architecture - By Thang Pham, Senior Software En...
Magento 2 Development Best Practices
MidwestPHP - Getting Started with Magento 2
Magento 2 Modules are Easy!
12 Amazing Features of Magento 2
The journey of mastering Magento 2 for Magento 1 developers
Madison PHP - Getting Started with Magento 2
Magento 2: A technical overview
[DevDay2019] Micro Frontends Architecture - By Thang Pham, Senior Software En...

What's hot (20)

PPTX
Magento 2 looks like.
PDF
Guillaume Thibaux - Can we win the fight against performance bottlenecks? Les...
PPTX
Integrate any Angular Project into WebSphere Portal
PPTX
Meet Magento Belarus - Sergey Ivashchenko
PPTX
.Net vs JAVA: Which One To Choose In 2022
PPTX
Developing Web Application Using J2EE - Nex
PPTX
Node js vs golang -which one is better ?
PDF
Micro Frontends Architecture - Jitendra kumawat (Guavus)
PDF
Learn .net and develop the web applications
PPT
Building Composite Applications with Silverlight - Prism 2.0
PPTX
PPTX
Technource - Zend framework Developers India
PDF
Asp net-certification-course-syllabus
PPTX
Meet Magento Belarus 2015: Mladen Ristić
PPTX
Web dynpro & ui5
PDF
Web development meetingup
PDF
Micro Frontends
PPTX
Patna MuleSoft Meetup | Exposing WEB GUI for DWL Mapping File
PPTX
Micro Front Ends : Divided We Rule by Parth Ghiya - AhmedabadJS
PPTX
Continuous workflow for a large react native app - mobile at wix
Magento 2 looks like.
Guillaume Thibaux - Can we win the fight against performance bottlenecks? Les...
Integrate any Angular Project into WebSphere Portal
Meet Magento Belarus - Sergey Ivashchenko
.Net vs JAVA: Which One To Choose In 2022
Developing Web Application Using J2EE - Nex
Node js vs golang -which one is better ?
Micro Frontends Architecture - Jitendra kumawat (Guavus)
Learn .net and develop the web applications
Building Composite Applications with Silverlight - Prism 2.0
Technource - Zend framework Developers India
Asp net-certification-course-syllabus
Meet Magento Belarus 2015: Mladen Ristić
Web dynpro & ui5
Web development meetingup
Micro Frontends
Patna MuleSoft Meetup | Exposing WEB GUI for DWL Mapping File
Micro Front Ends : Divided We Rule by Parth Ghiya - AhmedabadJS
Continuous workflow for a large react native app - mobile at wix
Ad

Viewers also liked (7)

PDF
Php Dependency Management with Composer ZendCon 2016
PDF
Demystifying Object-Oriented Programming - ZendCon 2016
PPTX
Mage Titans USA 2016 M2 deployment
PPTX
Mage Titans USA 2016 Magento/Facebook/RabbitMQ
PDF
DB2 and PHP in Depth on IBM i
PDF
All the Laravel things: up and running to making $$
PDF
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Php Dependency Management with Composer ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
Mage Titans USA 2016 M2 deployment
Mage Titans USA 2016 Magento/Facebook/RabbitMQ
DB2 and PHP in Depth on IBM i
All the Laravel things: up and running to making $$
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Ad

Similar to Your First Magento 2 Module (20)

PDF
Magento 2 Module in 50 Minutes
PDF
Magento 2 - An Intro to a Modern PHP-Based System - ZendCon 2015
PDF
Magento 2 Development for PHP Developers
PDF
Magento 2: New and Innovative? - php[world] 2015
PPTX
Advent of magento2
PDF
Mli 2017 technical intro to magento 2
PDF
Zepplin_Pronko_Magento_Festival Hall 1_Final
PDF
Magento 2 Backend Development Essentials
PPTX
Backward Compatibility Developer's Guide in Magento 2
PPT
Meet Magento Belarus - Magento2: What to expect and when? - Elena Leonova
PPTX
Backward Compatibility Developer's Guide in Magento 2. #MM17CZ
PPTX
API design best practices
PPT
Meet Magento Belarus - Elena Leonova
PPTX
Applying Code Customizations to Magento 2
PPTX
Igor Miniailo - Magento 2 API Design Best Practices
PDF
Introduction to Magento 2 module development - PHP Antwerp Meetup 2017
PPTX
Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
PPTX
MageConf 2017, Design API Best Practices
PDF
Magento Meetup Mancheter with PushON: Elena Leonova
PPTX
php[world] Magento101
Magento 2 Module in 50 Minutes
Magento 2 - An Intro to a Modern PHP-Based System - ZendCon 2015
Magento 2 Development for PHP Developers
Magento 2: New and Innovative? - php[world] 2015
Advent of magento2
Mli 2017 technical intro to magento 2
Zepplin_Pronko_Magento_Festival Hall 1_Final
Magento 2 Backend Development Essentials
Backward Compatibility Developer's Guide in Magento 2
Meet Magento Belarus - Magento2: What to expect and when? - Elena Leonova
Backward Compatibility Developer's Guide in Magento 2. #MM17CZ
API design best practices
Meet Magento Belarus - Elena Leonova
Applying Code Customizations to Magento 2
Igor Miniailo - Magento 2 API Design Best Practices
Introduction to Magento 2 module development - PHP Antwerp Meetup 2017
Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
MageConf 2017, Design API Best Practices
Magento Meetup Mancheter with PushON: Elena Leonova
php[world] Magento101

More from Ben Marks (7)

PDF
PWA for PHP Developers
PDF
Open Source Commerce Melee
PPTX
A World Without PHP
PDF
Magento 2: Modernizing an eCommerce Powerhouse
PDF
eCommerce and Open Source: Pot, PHP, and Unlimited Potential
PPTX
Open Source eCommerce: PHP Power
PPTX
Finding Your Way: Understanding Magento Code
PWA for PHP Developers
Open Source Commerce Melee
A World Without PHP
Magento 2: Modernizing an eCommerce Powerhouse
eCommerce and Open Source: Pot, PHP, and Unlimited Potential
Open Source eCommerce: PHP Power
Finding Your Way: Understanding Magento Code

Recently uploaded (20)

PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Essential Infomation Tech presentation.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
medical staffing services at VALiNTRY
PDF
Digital Strategies for Manufacturing Companies
PPTX
ai tools demonstartion for schools and inter college
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Transform Your Business with a Software ERP System
PDF
System and Network Administraation Chapter 3
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
System and Network Administration Chapter 2
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms I-SECS-1021-03
Essential Infomation Tech presentation.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Reimagine Home Health with the Power of Agentic AI​
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
medical staffing services at VALiNTRY
Digital Strategies for Manufacturing Companies
ai tools demonstartion for schools and inter college
CHAPTER 2 - PM Management and IT Context
Design an Analysis of Algorithms II-SECS-1021-03
Transform Your Business with a Software ERP System
System and Network Administraation Chapter 3
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
System and Network Administration Chapter 2
Operating system designcfffgfgggggggvggggggggg
How Creative Agencies Leverage Project Management Software.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
2025 Textile ERP Trends: SAP, Odoo & Oracle

Your First Magento 2 Module

  • 1. @benmarks ZendCon 2016 Intro to Magento 2 Your First Module
  • 3. @benmarks ZendCon 2016 About Ben • 2008-14: Developer, Director @ Blue Acorn • 2011-15: Trainer @ Magento U • 2014-now: Magento Evangelist @ Magento
  • 5. @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)
  • 6. @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
  • 7. @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
  • 8. @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.
  • 9. @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.
  • 11. @benmarks ZendCon 2016 M2 Module Structure Presentation logic 
 Request handling Configuration Rendering Exposes to app
  • 12. @benmarks ZendCon 2016 Module Registration app/code/BenMarks/Brands/
 
 etc/module.xml
 
 
 
 
 registration.php
  • 13. @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
  • 14. @benmarks ZendCon 2016 Configure a Frontend Route etc/frontend/routes.xml http://site.com/brands/ unique identifier BenMarks/Brands/Controller/
  • 15. @benmarks ZendCon 2016 Controller Time (1/3) BenMarks/Brands/Controller/Index/Index.php
  • 16. @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
  • 17. @benmarks ZendCon 2016 Controller Time (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 Setup Script (1/3) BenMarks/Brands/Setup/InstallSchema.php
  • 22. @benmarks ZendCon 2016 Setup Script (2/3) • According to Magento, our module is already installed!
 Need to remove module registration
 
 
 
 
 
 
 
 • bin/magento setup:upgrade
  • 23. @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
  • 24. @benmarks ZendCon 2016 Rendering a 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/