2017 © Pedro Peláez
 

library magento2-di-recipes

DI recipes for Magento 2

image

yireo-training/magento2-di-recipes

DI recipes for Magento 2

  • Sunday, June 24, 2018
  • by yireo-training
  • Repository
  • 1 Watchers
  • 1 Stars
  • 57 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 27 % Grown

The README.md

Magento 2 DI Recipes

This repository contains various recipes for using DI (Dependency Injection in your classes) to insert certain functionality., (*1)

This is NOT a Magento extension. It simply contains code samples., (*2)

Installation

composer config repositories.yireo-training-di-examples vcs git@github.com:yireo-training/magento2-di-recipes.git
composer require yireo-training/magento2-di-recipes:dev-master

Basic usage

Within a Magento 2 class, you can use DI via the constructor to inject yourself with the dependencies that you need. For any of these classes counts that if the parent class already has such a dependency injected, that you should use that injected dependency instead. For instance, if the parent offers a $context variable, inspect it to see if it offers what you need., (*3)

Working with the registry

To work with the registry, you would inject yourself with an instance of \Magento\Framework\Registry., (*4)

  • Yireo\DiRecipes\ViewModel\Registry

Working with the layout

Within Block classes, the $context variable is used to insert an instance of the layout in the variable $this->_layout, which can also be fetched using $this->getLayout(). Alternatively, if you are in an observer or alike, instantiate yourself with \Magento\Framework\View\LayoutFactory., (*5)

  • Yireo\DiRecipes\Block\LayoutExample
  • Yireo\DiRecipes\Observer\LayoutExample

Working with URLs

To inject URLs, you could inject \Magento\Framework\UrlInterface. However, it is much safer to get clean instances by injecting \Magento\Framework\UrlFactory instead. The current URL could be fetched using \Magento\Store\Model\StoreManagerInterface., (*6)

  • Yireo\DiRecipes\ViewModel\UrlExample
  • Yireo\DiRecipes\ViewModel\StoreManagerExample

The Versions

24/06 2018

dev-master

9999999-dev

DI recipes for Magento 2

  Sources   Download

OSL-3.0

The Requires

  • magento/framework ^100.1|^101.0|^102.0

 

The Development Requires

by Jisse Reitsma (Yireo)

composer-installer magento

14/06 2017

0.0.1

0.0.1.0

DI recipes for Magento 2

  Sources   Download

OSL-3.0

The Requires

  • php >=5.6.0

 

The Development Requires

by Jisse Reitsma (Yireo)

composer-installer magento