yireo-training / magento2-di-recipes
DI recipes for Magento 2
Installs: 75
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/yireo-training/magento2-di-recipes
Requires
- php: >=5.6.0
Requires (Dev)
- composer/composer: *@dev
This package is auto-updated.
Last update: 2025-10-28 05:57:43 UTC
README
This repository contains various recipes for using DI (Dependency Injection in your classes) to insert certain functionality.
This is NOT a Magento extension. It simply contains code samples.
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.
Working with the registry
To work with the registry, you would inject yourself with an instance of \Magento\Framework\Registry.
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.
Yireo\DiRecipes\Block\LayoutExampleYireo\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.
Yireo\DiRecipes\ViewModel\UrlExampleYireo\DiRecipes\ViewModel\StoreManagerExample