+ All Categories
Home > Software > Magento 2 Development for PHP Developers

Magento 2 Development for PHP Developers

Date post: 16-Apr-2017
Category:
Upload: joshua-warren
View: 232 times
Download: 6 times
Share this document with a friend
83
2 0 1 6 2 0 1 6
Transcript
Page 1: Magento 2 Development for PHP Developers

20162016

Page 2: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Magento 2 Development for PHP Developers

Page 3: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Is Your Environment Ready?

Page 4: Magento 2 Development for PHP Developers

@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

Page 5: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

About Me

Page 6: Magento 2 Development for PHP Developers

@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

Page 7: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

About This Tutorial

Page 8: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Assumptions

• You’re a PHP developer

• You have little-to-no experience with Magento 1 or 2

• You have a laptop and a desire to learn about Magento 2

Page 9: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Goals

• Knowledge of the basics of M2 development

• A clear path to learn more

• An invitation to the Magento development community

Page 10: Magento 2 Development for PHP Developers

@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!

Page 11: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

About You

Page 12: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

About You

• Any Magento 1 experience?

• Any Magento 2 experience?

• PHP 7 experience?

Page 13: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

About You

• Experience with other frameworks?

• Composer?

• Namespaces?

Page 14: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

About You

• Dependency injection?

• Service layers?

Page 15: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Page 16: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

About You

• Looking for detailed code examples?

• Want to get your hands dirty with some code today?

• Interested more in theory?

Page 17: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Useful Tools & Sites

Page 18: Magento 2 Development for PHP Developers

@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

Page 19: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Social Media for a New M2 Dev

• Twitter - hashtag #realmagento

• Magento’s Developer Evangelist - @benmarks

• Alan Storm - @alanstorm

• And many, many others…

Page 20: Magento 2 Development for PHP Developers

@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

Page 21: Magento 2 Development for PHP Developers

@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.

Page 22: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Technical Architecture of Magento 2

Page 23: Magento 2 Development for PHP Developers
Page 24: Magento 2 Development for PHP Developers

@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

Page 25: Magento 2 Development for PHP Developers

@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

Page 26: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

PSR’s All The Way Down

• PSR-0 thru PSR-4 (Autoloader)

Page 27: Magento 2 Development for PHP Developers
Page 28: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

phpunit, Selenium, JMeter, Jasmine and more all built in

Page 29: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Advanced Front-end Tech• LESS CSS (And SASS*)

• jQuery

• RequireJS

• Gulp.js

• Magento UI Library

Page 30: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Layers upon layers…

Page 31: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Layers

• Presentation layer

• Service Layer

• Domain Layer

• Persistence Layer

Page 33: Magento 2 Development for PHP Developers

@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.

Page 34: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Dependency Injection

• Reduces dependencies

• Promotes loose coupling

• Improves testability

Page 35: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Dependency Injection

• Magento 2 uses the Constructor Injection pattern of DI

• Automatic Dependency Injection

• Handled in Magento 2 via XML files

Page 36: Magento 2 Development for PHP Developers

@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']);}

Page 37: Magento 2 Development for PHP Developers

@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']);}

Page 38: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Interceptors

• Calls to almost any module can be intercepted and altered

• Possible to intercept before, after or around a function

Page 39: Magento 2 Development for PHP Developers

@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

Page 40: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Helpful Less-Technical M2 Concepts

• Built-in CMS system

• Static Blocks

• Widgets

Page 41: Magento 2 Development for PHP Developers

@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

Page 42: Magento 2 Development for PHP Developers

@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

Page 43: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Thoughts & Questions So Far?

Page 44: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Setting Up a New Magento 2 Site

Page 45: Magento 2 Development for PHP Developers

@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

Page 46: Magento 2 Development for PHP Developers

@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

Page 47: Magento 2 Development for PHP Developers

@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

Page 48: Magento 2 Development for PHP Developers

@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

Page 49: Magento 2 Development for PHP Developers

@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

Page 50: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Writing a Magento 2 Module

Page 51: Magento 2 Development for PHP Developers

@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

Page 52: Magento 2 Development for PHP Developers

@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

Page 53: Magento 2 Development for PHP Developers

@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

Page 54: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

composer.json{ "name": "magento/sample-module-minimal", "description": "A minimal skeleton Magento 2 module", "type": "magento2-module", "version": "1.0.0", "require": { "php": "~5.5.0|~5.6.0|~7.0.0" }, "autoload": { "files": [ "registration.php" ], "psr-4": { "Magento\\SampleMinimal\\": "" } } }

Page 55: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

registration.php

<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magento_SampleMinimal', __DIR__ );

Page 56: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

etc/module.xml

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Magento_SampleMinimal" setup_version="2.0.0"> </module> </config>

Page 57: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

etc/di.xml<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\Console\CommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="write_yaml_command" xsi:type=“object">

Creatuity\BlackfireCommands\Console\Command\WriteYamlCommand </item>

<item name=“profile_category_command" xsi:type=“object"> Creatuity\BlackfireCommands\Console\Command\ProfileCategoryCommand </item> </argument> </arguments> </type></config>

Page 59: Magento 2 Development for PHP Developers

@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.

Page 60: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Questions?

Page 61: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Designing Magento 2 Sites

Page 62: Magento 2 Development for PHP Developers

@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.

Page 63: Magento 2 Development for PHP Developers

@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

Page 64: Magento 2 Development for PHP Developers

@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.

Page 65: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Sample

• https://github.com/ubertheme/crafts-magento-2-theme

Page 66: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Questions?

Page 67: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Deploying Magento 2 Sites

Page 68: Magento 2 Development for PHP Developers
Page 69: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

No, seriously, hold on…

Page 70: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

The official Magento 2 documentation on deploying to production…

Page 71: Magento 2 Development for PHP Developers
Page 72: Magento 2 Development for PHP Developers
Page 73: Magento 2 Development for PHP Developers

@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

Page 74: Magento 2 Development for PHP Developers

@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

Page 75: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Questions?

Page 76: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Examples & Exercises

Page 77: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Real World Examples

• http://panhandleww.com/

• http://stickyholsters.com

Page 78: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Exercises

• What do you want to try in Magento 2?

Page 79: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Next Steps

Page 80: Magento 2 Development for PHP Developers

@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

Page 81: Magento 2 Development for PHP Developers

@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

Page 82: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Stay in Touch

• Never hesitate to ask questions via Twitter - @JoshuaSWarren or on Stack Exchange

• Questions today?

Page 83: Magento 2 Development for PHP Developers

@JoshuaSWarren #phpworld

Go build something awesome!


Recommended