We, the developers invested much time to get proficient in the art of building extensions in Magento 1. Magento 2 is ist just around the corner. And now? Do we have to start all over again?
This talk aims to show developers how features and techniques from Magento 1 translate to Magento 2.
The goal of a direct comparison between M1 and M2 is to ease the first steps with the new system.
Visit my German blog for more information on Magento 1 & Magento 2!
http://www.matthias-zeis.com
5. 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
6. Key concepts to grasp
• Decoupling modules
• Organising modules
• Splitting up
• Cleaning up
• Improving stability
• Improving quality
23. 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/mo
dule.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!
24. Concepts applied
• Organising modules
• Packaging modules
• All files in the module directory
• Improving quality
• Automated validation
• Automated testing
28. 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
30. Controllers & Route: Magento 2
2. Create controller
app/code/Mzeis/Mm15nl/Controller/Index/Index.php
<?php
namespace MzeisMm15nlControllerIndex;
class Index extends MagentoFrameworkAppActionAction
{
/* see next slide */
}
One controller, one directory
One action per file
31. Controllers & Route: Magento 2
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkViewResultPageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* @return MagentoFrameworkViewResultPage
*/
public function execute()
{
return $this->resultPageFactory->create();
}
Dependency Injection
32. Concepts applied
• Decoupling modules
• Dependency Injection
• Separation of concerns
• Splitting up
• XML configuration files
• Controller actions
41. 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!
42. 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!
43. Reading store configuration
Magento 1
public function getTitle() { return Mage::getStoreConfig('mzeis_mm15nl/talks/title'); }
Magento 2
public function __construct(
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}
public function getTitle() {
return $this->scopeConfig->getValue('mzeis_mm15nl/talks/title');
}
Dependency Injection,
Public API
45. Loading a product by SKU
Magento 1
$product = Mage::getModel('catalog/product');
$product->load($product->getIdBySku($sku));
Magento 2
public function __construct(
MagentoCatalogApiProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}
$this->productRepository->get($sku);
Dependency Injection
Service contracts
Public API
46. Concepts applied
• Decoupling modules
• Dependency injection
• Separation of concerns
• Improving stability
• Plug-ins (interception)
• Service contracts
• Public API