56

Building Magento 2 extensions 101 for Magento 1 developers

Embed Size (px)

Citation preview

Matthias Zeis Building Magento 2 extensions 101 for Magento 1 developers

(Vienna, AT)

Magento Certified Developer

@mzeis

matthias-zeis.com

What is this talk about?

• I know Magento 1 • I want to know Magento 2 • Do I have to start all over again?

https://twitter.com/allanmacgregor/status/554659836999110656

Goal of this talk

• Jump start for developers knowing Magento 1 • “I did X in this way in M1, how do I do it in M2?”

• Disclaimer: based on 0.74.0-beta 10

Key concepts to grasp

• Decoupling modules • Organising modules • Splitting up • Cleaning up • Improving stability • Improving quality

Let's get started!

Create an extension

Create an extension

1. Define the extension 2. Activate the extension

Create an extension: Magento 1

1. Define the extension: Configuration XML file app/code/{core,community,local}/Mzeis/Mm15nl/etc/config.xml <?xml version="1.0"?> <config> <modules> <Mzeis_Mm15nl> <version>1.0.0</version> </Mzeis_Mm15nl> </modules> </config>

Extension & DB schema version

Create an extension: Magento 1

2. Activate the extension: Activation XML file app/etc/modules/Mzeis_Mm15nl.xml <?xml version="1.0"?> <config> <modules> <Mzeis_Mm15nl> <active>true</active> <codePool>community</codePool> </Mzeis_Mm15nl> </modules> </config>

And now in Magento 2!

Create an extension: Magento 2

1. Define the extension: Configuration XML file app/code/Mzeis/Mm15nl/etc/module.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">

<module name="Mzeis_Mm15nl" setup_version="1.0.0" /> </config>

DB schema version

Create an extension: Magento 2

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">

What the…?

Autocompletion & validation!

Create an extension: Magento 2

2. Activate the extension: CLI tool modifies app/etc/config.php php bin/magento module:enable Mzeis_Mm15nl

Well done!

Concepts applied

• Organising modules • All files in the module directory

• Cleaning up • Shortening code

• Improving quality • Automated validation • Automated testing

Organising modules

Organising modules: Magento 1

• File organisation • Core: put files into the appropriate directories • Community: modman, Composer + Composer installer

• Only hard dependencies

• Load order • dependencies • alphabet

Organising modules: Magento 1

• Configure dependencies + load order app/etc/modules/Mzeis_Mm15nl.xml <?xml version="1.0"?> <config> <modules> <Mzeis_Mm15nl> <active>true</active> <codePool>community</codePool> <depends> <Mage_Catalog /> </depends> </Mzeis_Mm15nl> </modules> </config>

Hard dependency

Organising modules: Magento 2

• File organisation • Core: Composer + Composer installer • Community: ... okay with that?

• Hard & soft dependencies

• Load order • sequence configuration • alphabet

{ "name": "mzeis/mm15nl", "description": "Meet Magento 15 NL", "require": { "magento/module-store": "0.74.0-beta10" }, "suggest": { "magento/module-cookie": "0.74.0-beta10" },

"type": "magento2-module", "version": "1.0.0", "extra": { "map": [ [ "*", "Mzeis/Mm15nl" ] ] } }

Module types

Mapping

Hard dependency

Soft dependency

Organising modules: Magento 2

• Configure dependencies app/code/Mzeis/Mm15nl/composer.json

Extension version

Organising modules: Magento 2

• Configure load order app/code/Mzeis/Mm15nl/etc/module.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">

<module name="Mzeis_Mm15nl" setup_version="1.0.0"> <sequence> <module name="Magento_Catalog" /> </sequence> </module> </config>

Load order of one or multiple modules No error when module is missing!

Concepts applied

• Organising modules • Packaging modules • All files in the module directory

• Improving quality

• Automated validation • Automated testing

Controller & Route

Controllers & Route

1. Define route 2. Create controller

Controllers & Route: Magento 1

1. Define route app/code/community/Mzeis/Mm15nl/etc/config.xml <config> <frontend> <routers> <mzeis_mm15nl> <use>standard</use> <args> <frontName>mm15nl</frontName> <module>Mzeis_Mm15nl</module> </args> </mzeis_mm15nl> </routers> </frontend>

Controllers & Route: Magento 1

2. Create controller app/code/community/Mzeis/Mm15nl/controllers/IndexController.php <?php class Mzeis_Mm15nl_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { $this->loadLayout(); $this->renderLayout(); } }

Multiple actions in one file

One controller, one file

Controllers & Route: Magento 2

1. Define route app/code/Mzeis/Mm15nl/etc/frontend/routes.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">

<router id="standard"> <route id="mzeis_mmnl" frontName="mm15nl"> <module name="Mzeis_Mm15nl" /> </route> </router> </config>

No numbers allowed as of 0.74.0-beta10 (#1290)!

Controllers & Route: Magento 2

2. Create controller app/code/Mzeis/Mm15nl/Controller/Index/Index.php <?php namespace Mzeis\Mm15nl\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { /* see next slide */ }

One controller, one directory

One action per file

Controllers & Route: Magento 2

public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory ) { $this->resultPageFactory = $resultPageFactory; parent::__construct($context); } /** * @return \Magento\Framework\View\Result\Page */ public function execute() { return $this->resultPageFactory->create(); }

Dependency Injection

Concepts applied

• Decoupling modules • Dependency Injection • Separation of concerns

• Splitting up • XML configuration files • Controller actions

Concepts applied

• Cleaning up • Separating e-commerce application from framework

• Improving quality • Automated validation • Automated testing

Layout & Design

Layout & Design: Magento 1

• Extension layout file path

app/design/ {adminhtml,frontend,install}/ rwd/ default/ layout/ mzeis_mm15nl.xml

Area

Package

Theme

Extension layout file

Layout & Design: Magento 2

• Extension layout file path

app/code/ Mzeis/ Mm15nl/ view/ {adminhtml,base,frontend,install}/ layout/ mzeis_mmnl_index_index.xml

Vendor

Extension

Area

Layout handle file

Remember #1290!

Layout & Design: Magento 1

• Extension layout file directives <layout version="0.1.0"> <mzeis_mm15nl_index_index> <reference name="content"> <block type="core/text_list" name="mzeis.mm15nl.container" /> <block type="mzeis_mm15nl/talks" name="mzeis.mm15nl.talks" /> </reference> <reference name="root"> <action method="setTemplate"> <template>page/empty.phtml</template> </action> </reference> </mzeis_mm15nl_index_index> </layout>

Container-ish

Class

Modify existing block

Layout handle

Layout & Design: Magento 2

• Extension layout file directives <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left"

xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">

<body> <container name="mzeis.mm15nl.container" /> <block class="Mzeis\Mm15nl\Block\Talks" name="mzeis.mm15nl.talks" /> <referenceContainer name="root"> <block class="Mzeis\Mm15nl\Block\Info" name="mzeis.mm15nl.info" /> </referenceContainer> </body>

Real container

Class

Modify existing cont.

Concepts applied

• Organising modules • All files in the module directory

• Splitting up • XML layout files

• Cleaning up • Renaming

Interacting with other modules

Interacting with other modules: M1

• Using functionality • Get object from “god class” Mage

• Modifying behaviour • Event observers • Rewrite classes • Code pool overrides

• No stable API - everything can change without notice!

Interacting with other modules: M2

• Using functionality • Dependency Injection • Service contracts

• Modifying behaviour • Plug-ins (interception) • Event observers • Rewrite classes

• Public API & SPI - promised to be stable for minor releases!

Reading store configuration

Magento 1 public function getTitle() { return Mage::getStoreConfig('mzeis_mm15nl/talks/title'); }

Magento 2 public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { $this->scopeConfig = $scopeConfig; } public function getTitle() { return $this->scopeConfig->getValue('mzeis_mm15nl/talks/title'); }

Dependency Injection, Public API

Logging

Magento 1 Mage::logException($e);

Magento 2 public function __construct( \Psr\Log\LoggerInterface $logger ) { $this->logger = $logger; } $this->logger->critical($e);

Dependency Injection PSR-3 compliant logger! Magento\Framework\Logger\Monolog

Loading a product by SKU

Magento 1 $product = Mage::getModel('catalog/product'); $product->load($product->getIdBySku($sku));

Magento 2 public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productRepository ) { $this->productRepository = $productRepository; } $this->productRepository->get($sku);

Dependency Injection Service contracts Public API

Concepts applied

• Decoupling modules • Dependency injection • Separation of concerns

• Improving stability

• Plug-ins (interception) • Service contracts • Public API

Conclusion

Decoupling modules

• Dependency Injection • Separation of concerns

Organising modules

• Packaging modules • All files in the module directory

Splitting up

• XML configuration files • XML layout files • Controller actions

Cleaning up

• Separating e-commerce application from framework • Shortening code • Renaming

Improving stability

• Plug-ins (interception) • Service contracts • Public API

Improving quality

• Automated validation • Automated testing

Resources

• Code github.com/magento/magento2 • Sample modules github.com/magento/magento2-samples • Documentation devdocs.magento.com • Developer Hub magento.com/developers/magento2 • Fundamentals of magento.com/training/

Magento 2 Development

Resources

• Alan Kent alankent.wordpress.com • Max Yekaterynenko maxyek.wordpress.com • Ben Marks bhmarks.com/blog/

Thank you! Questions?

Slides slideshare.net/mzeis/ M1 github.com/mzeis/mm15nl-magento1/ M2 github.com/mzeis/mm15nl-magento2/

@mzeis matthias-zeis.com

We're hiring! limesoda.com/jobs/