PHPackages                             eryw/pastry-bag - 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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. eryw/pastry-bag

ActiveCakephp-plugin[PSR &amp; Standards](/categories/psr-standards)

eryw/pastry-bag
===============

A plugin for CakePHP 3 provides the constructor dependencies injection and the controller method injection

v1.0.1(9y ago)263MITPHP

Since Sep 25Pushed 9y ago1 watchersCompare

[ Source](https://github.com/eryw/pastry-bag)[ Packagist](https://packagist.org/packages/eryw/pastry-bag)[ RSS](/packages/eryw-pastry-bag/feed)WikiDiscussions master Synced 4w ago

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

PastryBag
=========

[](#pastrybag)

[![Packagist](https://camo.githubusercontent.com/9eb3b3312a94387ca0d9193c4aea01e21563bb44388ac24a56e43f983784169e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f657279772f7061737472792d6261672e7376673f7374796c653d666c61742d737175617265)](LICENSE.txt)[![Travis branch](https://camo.githubusercontent.com/e540d2ee48995ea285287f503269c6cc3745288f2ed2fee45b6b6034b9b6648f/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f657279772f7061737472792d6261672f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/eryw/pastry-bag)[![Latest Stable Version](https://camo.githubusercontent.com/4b49cccb127814fd17acd4021c268a52d21aeb0cedd3d5311b93f2e320207672/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f657279772f7061737472792d6261672e7376673f7374796c653d666c61742d737175617265266c6162656c3d737461626c65)](https://packagist.org/packages/eryw/pastry-bag)

Dependencies injection plugin for CakePHP 3. This plugin provides a constructor injection and a controller action injection.

Installation
============

[](#installation)

#### Composer

[](#composer)

```
composer require eryw/pastry-bag=@stable
```

#### Configuration

[](#configuration)

Add the following line to your `config/bootstrap.php`:

```
Plugin::load('PastryBag', ['bootstrap' => true]);
```

##### For CakePHP 3.2 with dispatch filter

[](#for-cakephp-32-with-dispatch-filter)

Replace your controller factory filter in `config/bootstrap.php`:

```
DispatcherFactory::add('ControllerFactory');
```

With the following:

```
DispatcherFactory::add('PastryBag\Routing\Filter\ControllerFactoryFilter');
```

##### For CakePHP 3.3 with middleware

[](#for-cakephp-33-with-middleware)

Please override method `getDispatcher()` on your `Application` class

```
class Application extends BaseApplication
{
    // ... //

    protected function getDispatcher()
    {
        return new ActionDispatcher(new ControllerFactory(), null, DispatcherFactory::filters());
    }
}
```

To make the dependency injection works, all controller must extends `PastryBag\Controller\Controller`.

So, change your `AppController` parent class from `Cake\Controller\Controller` into `PastryBag\Controller\Controller`:

```
class AppController extends \PastryBag\Controller\Controller
{
    // ... //
}
```

Usage
=====

[](#usage)

#### Constructor Injection

[](#constructor-injection)

```
class UsersController extends AppController
{
    protected $payment;

    public function __construct(PaymentService $payment)
    {
        parent::__construct();
        $this->payment = $payment;
    }

    public function payBill()
    {
        // `$this->payment` will auto injected with instance of PaymentService
        $this->payment->anyMethodOfPaymentService();
    }
}
```

#### The controller method injection

[](#the-controller-method-injection)

```
class RemoteGaleryController extends AppController
{
    public function index($id, MyHttpClient $client)
    {
        // `$client` will auto injected with instance of MyHttpClient
        $client->request('GET', 'http://remotesite.com');
    }
}
```

##### Note:

[](#note)

Only the type hinted parameters will auto injected.

Config
======

[](#config)

This plugin uses [Aura.Di](https://github.com/auraphp/Aura.Di) as container. Configuration should be put inside a class that implements `Aura\Di\ContainerConfigInterface` and the list of configuration (class name or instance) must be put at `config/container_configs.php`

Example content of `config/container_configs.php`:

```
use Aura\Di\Container;
use Aura\Di\ContainerConfig;
use Cake\ORM\TableRegistry;

// OPTIONAL. You can move this class to other file if you want
class DiConfig extends ContainerConfig
{
    public function define(Container $di)
    {
        $di->set(\App\Model\Table\UsersTable::class, $di->lazy(function () {
            return TableRegistry::get('Users');
        }));
        $di->types[\App\Model\Table\UsersTable::class] = $di->lazyGet(\App\Model\Table\UsersTable::class);
    }
}

// REQUIRED. This file must return list of configs as array
return [
    'My\Awesome\App\ClassImplementsAuraDiContainerConfigInterface',
    new DiConfig,
];
```

For advance container configuration and usage, please check [Aura.Di](https://github.com/auraphp/Aura.Di) official documentation

If you want to access the instance of container directly, you can use static method `PastryBag::getContainer()`:

```
// `$di` is instance of Aura\Di\Container
$di = PastryBag::getContainer();
```

This plugin is inspired by [PipingBag](https://github.com/lorenzo/piping-bag) plugin but does not require annotations.

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

3565d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2015638?v=4)[Ery Widyanto](/maintainers/eryw)[@eryw](https://github.com/eryw)

---

Top Contributors

[![eryw](https://avatars.githubusercontent.com/u/2015638?v=4)](https://github.com/eryw "eryw (15 commits)")

---

Tags

containerdependencyinjectioncakephpioc

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/eryw-pastry-bag/health.svg)

```
[![Health](https://phpackages.com/badges/eryw-pastry-bag/health.svg)](https://phpackages.com/packages/eryw-pastry-bag)
```

###  Alternatives

[league/container

A fast and intuitive dependency injection container.

86892.2M397](/packages/league-container)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2859.2k2](/packages/capsule-di)[miladrahimi/phpcontainer

Dependency injection (IoC) container for PHP projects

1323.5k2](/packages/miladrahimi-phpcontainer)[slince/di

A flexible dependency injection container

20268.4k6](/packages/slince-di)[bedita/manager

BEdita Manager - official admin webapp for BEdita4 API

131.1k](/packages/bedita-manager)

PHPackages © 2026

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