As one of the most popular eCommerce platforms in the world, the demand for Magento developer is at an all-time high. In this tutorial, I’ll introduce you to developing for Magento 2 and help you get up to speed quickly so that you’re ready to support and extend existing Magento 2 sites.
Presented as a 3.5 hour tutorial at php[world] 2016.
4. @JoshuaSWarren #phpworld
MageScotch
• How many of you downloaded it in advance?
• Anyone need help?
• If you haven’t installed it, follow the instructions at bit.ly/
2eYkhon
6. @JoshuaSWarren #phpworld
About Me
• PHP-Based Ecommerce Developer Since 1999
• Magento Developer Since 2008; Magento 2 Developer Since
2014
• Magento Master
• Founder & CEO of Creatuity Corp, Magento Enterprise
Solution Partner
10. @JoshuaSWarren #phpworld
Style
• Light-hearted & open - have fun + don’t hesitate to be blunt
• Interactive - do not hold questions to the end
• Customized - want more code? More theory? Just ask!
16. @JoshuaSWarren #phpworld
About You
• Looking for detailed code examples?
• Want to get your hands dirty with some code today?
• Interested more in theory?
18. @JoshuaSWarren #phpworld
Useful Sites for a New M2 Dev
• Magento DevDocs - devdocs.magento.com
• Magento Stack Exchange - magento.stackexchange.com
• github.com/Creatuity/LearningMagento2
• Alan Storm’s Sites - alanstorm.com + magento-
quickies.alanstorm.com
19. @JoshuaSWarren #phpworld
Social Media for a New M2 Dev
• Twitter - hashtag #realmagento
• Magento’s Developer Evangelist - @benmarks
• Alan Storm - @alanstorm
• And many, many others…
20. @JoshuaSWarren #phpworld
Useful Tools for a New M2 Dev
• IDE: PHPStorm
• Magicento or JetBrains Magento 2 Plugin
• A Vagrant or Docker image for local development
• Sample code: github.com/magento/magento2-samples
21. @JoshuaSWarren #phpworld
Useful Tools for a New M2 Dev
• Magento 2 is a constantly evolving platform. Keep your
skills up to date and keep an eye out for new tools to assist
you.
24. @JoshuaSWarren #phpworld
Beginner’s Mind
• If you have experience with Magento 1, try to set it aside
• Magento 2 rewards Shoshin - a beginner’s mind
• Start with the basic architectural concepts in Magento 2
25. @JoshuaSWarren #phpworld
Composer
• Magento 2 is built on top of Composer
• Each module/extension can and should be a Composer
module
• This includes each core module in the Magento 2 core
33. @JoshuaSWarren #phpworld
Dependency Injection
• Dependencies are injected into objects that need them
• “Don’t call us, we’ll call you”
• Instead of building an object in your class, it’s passed in via
your constructor, automatically.
36. @JoshuaSWarren #phpworld
Without Dependency Injection
public function getFormattedPrice($sku)
{
$db = new DBHandler;
$row = $db->query('SELECT price FROM products WHERE sku = ?', $sku);
$formatter = new PriceFormatter;
return $formatter->asDollars($row['price']);
}
37. @JoshuaSWarren #phpworld
With Dependency Injection
public function getFormattedPrice($sku, $db, $formatter)
{
$row = $db->query('SELECT price FROM products WHERE sku = ?', $sku);
return $formatter->asDollars($row['price']);
}
39. @JoshuaSWarren #phpworld
Helpful Less-Technical M2 Concepts
• Magento can power multiple websites on one installation
• Websites -> Stores -> Store Views
• Translation & currency exchange system built in
41. @JoshuaSWarren #phpworld
Helpful Less-Technical M2 Concepts
• Modular product & customer attributes
• Pre-made extensions available on
marketplace.magento.com
• Two editions - Community Edition & Enterprise Edition
42. @JoshuaSWarren #phpworld
A Few Cautions
• Magento 2 is a work in progress
• Service contracts are incomplete
• Not all core code has been refactored
• Best practices are still being determined
• Check devdocs, Stack Exchange best-practice tag, blogs &
presentations
45. @JoshuaSWarren #phpworld
Multiple Options
• Easy Install: Download zip file, run web installer
• System Integrator: Composer-based, run web or CLI installer
• Contributing Developer: Clone Magento 2 repository
46. @JoshuaSWarren #phpworld
Start with a Virtual Machine
• MageScotch follows the contributing developer approach to
Magento 2 development
• Magento 2 repository is checked out to /opt/magento2
• It’s then copied to /var/www/public/magento2
47. @JoshuaSWarren #phpworld
Start with a Virtual Machine
• Starting with a VM simplifies the process and ensures you
have a working environment
• Provides you with the additional tools you need such as the
proper PHP version (7) and Composer
48. @JoshuaSWarren #phpworld
Starting an M2 Project
• Understand your end goal for the project
• Take time to map business requirements to existing
Magento 2 functionality and modules
• Complete as much as you can in the admin panel
49. @JoshuaSWarren #phpworld
Starting an M2 Project
• Any business logic customizations should be completed via
a Magento 2 module
• Purely cosmetic changes should take place as a custom
theme
• Whatever you do, don’t edit core files
51. @JoshuaSWarren #phpworld
Magento 2 Modules
• Magento 2 modules can do anything
• Provide new shipping methods & payment gateway
integrations
• Implement entirely new ordering workflows
• Make simple, minor changes to functionality
52. @JoshuaSWarren #phpworld
Magento 2 Modules
• Magento 2 modules can be implemented within your
existing codebase - magento2/app/code/Namespace/
Module
• Magento 2 modules can also be freestanding modules with
their own Git repository you install using Composer
53. @JoshuaSWarren #phpworld
Magento 2 Modules
• All Magento 2 modules have a few things in common
• composer.json
• registration.php
• etc/module.xml
• Most also have etc/di.xml
59. @JoshuaSWarren #phpworld
Recommendations
• When building a new site, write many simple modules for
each distinct customization, not one large module
• Name modules in a logical manner based on the
functionality they provide
• Don’t reinvent the wheel. Use libraries on Packagist and
open source modules where possible.
62. @JoshuaSWarren #phpworld
Themes, Layouts and Templates
• A Magento website runs a theme
• Magento fully supports parent/child theme relationships
• Don’t edit the core theme. Create a new child theme that
inherits from the core Luma or Blank themes.
63. @JoshuaSWarren #phpworld
Themes, Layouts and Templates
• A theme consists of layouts and templates
• Layouts are XML files that explain what blocks and
containers appear on a page
• Templates are PHTML files that contain HTML markup for a
specific page or section of a page
64. @JoshuaSWarren #phpworld
Stylesheets & Preprocessing
• Magento 2 utilizes the LESS CSS preprocessor
• LESS allows you to use variables, mixins and rules in your
CSS
• Modify LESS files and then compile them into CSS. Don’t
modify CSS files directly.
73. @JoshuaSWarren #phpworld
One Option…
• Upload all files to your server
• bin/magento setup:upgrade
• bin/magento setup:di:compile
• bin/magento deploy:mode:set production
• bin/magento setup:static-content:deploy
74. @JoshuaSWarren #phpworld
Check Your Infrastructure
• Magento Enterprise supports Varnish out of the box - use it
• Please don’t deploy on $5/month hosting
• Configure Magento to use Redis for cache storage
80. @JoshuaSWarren #phpworld
php[world] sessions
• Magento 2 Performance Talk
• Wednesday, 3PM, Ash Grove C
• Automating Your Workflow With Gulp.js
• Thursday, 11:30AM, Great Falls
• Magento 2 Development Best Practices
• Friday, 10AM, Ash Grove A
81. @JoshuaSWarren #phpworld
Future Events
• Magento 2 Performance Training - January 18th-20th in
Orlando with Ivan Chepurnyi - http://bit.ly/2eAo8cz
• Magento Imagine 2017 - April 3rd-5th in Las Vegas -
imagine.magento.com
82. @JoshuaSWarren #phpworld
Stay in Touch
• Never hesitate to ask questions via Twitter -
@JoshuaSWarren or on Stack Exchange
• Questions today?