PHPackages                             yireo-training/magento2-di-recipes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yireo-training/magento2-di-recipes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yireo-training/magento2-di-recipes
==================================

DI recipes for Magento 2

0.0.1(8y ago)275OSL-3.0PHPPHP &gt;=5.6.0

Since Jun 14Pushed 7y ago2 watchersCompare

[ Source](https://github.com/yireo-training/magento2-di-recipes)[ Packagist](https://packagist.org/packages/yireo-training/magento2-di-recipes)[ RSS](/packages/yireo-training-magento2-di-recipes/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Magento 2 DI Recipes
====================

[](#magento-2-di-recipes)

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

[](#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
-----------

[](#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
-------------------------

[](#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
-----------------------

[](#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\LayoutExample`
- `Yireo\DiRecipes\Observer\LayoutExample`

Working with URLs
-----------------

[](#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\UrlExample`
- `Yireo\DiRecipes\ViewModel\StoreManagerExample`

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3256d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4a6a4e3ec0e7f3a8d2d223c90c5545319fc0a382afd3bc10cd910fedc0fa8f5d?d=identicon)[yireo-training](/maintainers/yireo-training)

---

Tags

magentocomposer-installer

### Embed Badge

![Health badge](/badges/yireo-training-magento2-di-recipes/health.svg)

```
[![Health](https://phpackages.com/badges/yireo-training-magento2-di-recipes/health.svg)](https://phpackages.com/packages/yireo-training-magento2-di-recipes)
```

###  Alternatives

[magento/magento-composer-installer

Composer installer for Magento modules

7523.3M318](/packages/magento-magento-composer-installer)[yireo/magento2-webp2

Magento 2 module to add WebP support to the Magento frontend

2091.2M7](/packages/yireo-magento2-webp2)[magento-hackathon/magento-composer-installer

Composer installer for Magento modules

214757.6k374](/packages/magento-hackathon-magento-composer-installer)[yireo/magento2-googletagmanager2

Add Google Tag Manager to Magento

167930.4k5](/packages/yireo-magento2-googletagmanager2)[yireo/magento2-extensionchecker

Scan the code of a Magento module

96151.6k3](/packages/yireo-magento2-extensionchecker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
