Upload
ben-marks
View
142
Download
0
Embed Size (px)
Citation preview
@benmarks ZendCon 2016
Intro to Magento 2Your First Module
@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 StructurePresentation logicRequest handling
Configuration
Rendering
Exposes to app
@benmarks ZendCon 2016
Module Registrationapp/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 upbin/magento module:status
• Enable module bin/magento module:enable BenMarks_Brands
@benmarks ZendCon 2016
Configure a Frontend Routeetc/frontend/routes.xml
http://site.com/brands/
uniqu
e iden
tifier
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 & refreshbin/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): ModelBenMarks/Brands/Model/Brand.php
@benmarks ZendCon 2016
ORM (2/3): Resource ModelBenMarks/Brands/Model/ResourceModel/Brand.php
@benmarks ZendCon 2016
ORM (3/3): CollectionBenMarks/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 List1. 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/