PHPackages                             t4web/actioninjections - 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. t4web/actioninjections

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

t4web/actioninjections
======================

ZF2 Module wich allows to inject dependencies in controller action

1.0.1(10y ago)01061BSD-3-ClausePHPPHP &gt;=5.4.0

Since Sep 14Pushed 10y ago2 watchersCompare

[ Source](https://github.com/t4web/ActionInjections)[ Packagist](https://packagist.org/packages/t4web/actioninjections)[ Docs](https://github.com/robaks/ActionInjections)[ RSS](/packages/t4web-actioninjections/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (6)Versions (3)Used By (0)

ActionInjections
================

[](#actioninjections)

Master: [![Build Status](https://camo.githubusercontent.com/78dc21836cd3ecb5e53bd6889d7bbce4bc43ddbf1d5d9fb5486b58b4bc8e1158/68747470733a2f2f7472617669732d63692e6f72672f74347765622f416374696f6e496e6a656374696f6e732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/t4web/ActionInjections)[![codecov.io](https://camo.githubusercontent.com/7aecd78e6ed1f02de38f5f656614de76c845587927e574cff5f609527e4b62ae/687474703a2f2f636f6465636f762e696f2f6769746875622f74347765622f416374696f6e496e6a656374696f6e732f636f7665726167652e7376673f6272616e63683d6d6173746572)](http://codecov.io/github/t4web/ActionInjections?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f3228045d5d334b05665c781b0997096663be89b0684e7cbc51954a071263833/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f74347765622f416374696f6e496e6a656374696f6e732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/t4web/ActionInjections/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/f52cfe6778ca165141e10681779fea7e0e14dd3ae0aa0a5471f62e611bdc0de4/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f62663037383663342d356630322d343837342d623363312d3637646133633335633430612f6d696e692e706e67)](https://insight.sensiolabs.com/projects/bf0786c4-5f02-4874-b3c1-67da3c35c40a)[![Dependency Status](https://camo.githubusercontent.com/396ba9a756f338439e8a3fac278f1159ff5d6aa9d2f2bc74df9ae2393419ecc5/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3535346530306566626236613165336264623030303030322f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/user/projects/554e00efbb6a1e3bdb000002)

Introduction
------------

[](#introduction)

ZF2 Module wich allows to inject dependencies in controller action for better incapsulate testing and remove controller dependency from ServiceLocator.

*Problem*: I have simply CRUD Controller, with actions "create", "update", "show", "delete", i have 3 dependency in action "delete": ViewModel, Request, some Service.

```
class AjaxController extends Zend\Mvc\Controller\AbstractActionController
{
    public function deleteTimesheetAction() {
        $view = new ViewModel();

        if (!$this->getRequest()->isPost()) {
            return $view;
        }

        $timesheetDeleteService = $this->getServiceLocator()->get('Timesheet\Timesheet\Service\Delete');

        $timesheetId = $this->getRequest()->getPost()->get('id', 0);
        if (!$timesheetDeleteService->delete($timesheetId)) {
            $view->errors = $timesheetDeleteService->getErrors();
        }

        return $view;
    }
    //...
}
```

in this case i can't easy test this controller, because:

1. I can't mock ViewModel (constructor calling)
2. Very difficult create test for this $this-&gt;getRequest()-&gt;getPost()-&gt;get('id', 0);
3. Nobody understand dependencies in current controller, because $this-&gt;getServiceLocator()-&gt;get('SomeService') inside controller - is bad practice

Ok, refactor it..

*Problem*: I have simply CRUD Controller, with actions "create", "update", "show", "delete", i have 3 dependency in action "delete": ViewModel, Request, some Service. For height testability i add all dependencies in Controller::\_\_constructor().

```
class AjaxController extends Zend\Mvc\Controller\AbstractActionController
{
    /**
     * @var BaseFinder
     */
    private $timesheetFinder;

    /**
     * @var BaseFinder
     */
    private $calendarFinder;

    /**
     * @var CreateInterface
     */
    private $createService;

    /**
     * @var UpdateInterface
     */
    private $updateService;

    /**
     * @var DeleteInterface
     */
    private $deleteService;

    /**
     * @var AjaxViewModel
     */
    private $view;

    public function __construct(
        BaseFinder $timesheetFinder,
        BaseFinder $calendarFinder,
        CreateInterface $timesheetCreateService,
        UpdateInterface $timesheetUpdateService,
        DeleteInterface $timesheetDeleteService,
        AjaxViewModel $view)
    {

        $this->timesheetFinder = $timesheetFinder;
        $this->calendarFinder = $calendarFinder;
        $this->createService = $timesheetCreateService;
        $this->updateService = $timesheetUpdateService;
        $this->deleteService = $timesheetDeleteService;
        $this->view = $view;
    }

    public function deleteTimesheetAction() {
        $view = new ViewModel();

        if (!$this->getRequest()->isPost()) {
            return $this->view;
        }

        $timesheetId = $this->getRequest()->getPost()->get('id', 0);
        if (!$this->deleteService->delete($timesheetId)) {
            $this->view->errors = $this->deleteService->getErrors();
        }

        return $this->view;
    }
    // ...
}
```

```
class AjaxControllerFactory implements FactoryInterface {

    public function createService(ServiceLocatorInterface $serviceLocator) {
        $serviceManager = $serviceLocator->getServiceLocator();
        return new AjaxController(
            $serviceManager->get('Timesheet\Timesheet\Service\Finder'),
            $serviceManager->get('Calendar\Calendar\Service\Finder'),
            $serviceManager->get('Timesheet\Timesheet\Service\Create'),
            $serviceManager->get('Timesheet\Timesheet\Service\Update'),
            $serviceManager->get('Timesheet\Timesheet\Service\Delete'),
            $serviceManager->get('Timesheet\Controller\ViewModel\AjaxViewModel')
        );
    }
}
```

in this case i can easy test this controller, but:

1. I have to big \_\_constructor (almost god object)
2. How many mock's i must create for test on method "delete"?
3. Nobody understand where i use each dependency in current controller
4. I must test ControllerFactory

*Solution*: use `t4web/ActionInjections`Add in your module.config.php section `controller_action_injections`

```
    'controller_action_injections' => array(
        'Timesheet\Controller\User\AjaxController' => array(
            'deleteTimesheetAction' => array(
                'request',
                'Timesheet\Controller\ViewModel\AjaxViewModel',
                'Timesheet\Timesheet\Service\Delete',
            ),
        ),
    ),
```

where `request`, `Timesheet\Controller\ViewModel\AjaxViewModel`, `Timesheet\Timesheet\Service\Delete` your dependecies, and just use it in your controller action

```
class AjaxController extends Zend\Mvc\Controller\AbstractActionController
{
    public function deleteTimesheetAction(HttpRequest $request, AjaxViewModel $view, DeleteInterface $timesheetDeleteService) {
        if (!$request->isPost()) {
            return $view;
        }

        $timesheetId = $request->getPost()->get('id', 0);
        if (!$timesheetDeleteService->delete($timesheetId)) {
            $view->setErrors($timesheetDeleteService->getErrors());
        }

        return $view;
    }
    //...
}
```

and test it:

```
class AjaxControllerTest extends \PHPUnit_Framework_TestCase {

    public function testDeleteTimesheetAction_Delete_ReturnView() {
        $requestMock = $this->getMockBuilder('Zend\Http\PhpEnvironment\Request')->disableOriginalConstructor()->getMock();
        $timesheetDeleteServiceMock = $this->getMockBuilder('T4webBase\Domain\Service\Delete')->disableOriginalConstructor()->getMock();
        $ajaxViewModel = new AjaxViewModel();

        $timesheetId = 1;
        $parameters = new Parameters(array('id' => $timesheetId));

        $requestMock->expects($this->once())
            ->method('isPost')
            ->will($this->returnValue(true));

        $requestMock->expects($this->once())
            ->method('getPost')
            ->will($this->returnValue($parameters));

        $timesheetDeleteServiceMock->expects($this->once())
            ->method('delete')
            ->with($this->equalTo($timesheetId))
            ->will($this->returnValue(true));

        $controller = new AjaxController();

        /** @var $result AjaxViewModel */
        $result = $controller->deleteTimesheetAction($requestMock, $ajaxViewModel, $timesheetDeleteServiceMock);

        $this->assertEquals($ajaxViewModel, $result);
    }
    //...
}
```

very fast, easy, readable, incapsulate unit test.

Requirements
------------

[](#requirements)

- [Zend Framework 2](https://github.com/zendframework/zf2) (latest master)

Installation
------------

[](#installation)

### Main Setup

[](#main-setup)

#### By cloning project

[](#by-cloning-project)

Clone this project into your `./vendor/` directory.

#### With composer

[](#with-composer)

Add this project in your composer.json:

```
"repositories": [
        {
            "type": "git",
            "url": "https://github.com/t4web/actioninjections.git"
        }
],

"require": {
    "t4web/actioninjections": "dev-master"
}
```

Now tell composer to download Authentication by running the command:

```
$ php composer.phar update
```

#### Post installation

[](#post-installation)

Not need enabling it in your `application.config.php`file, just extends from `T4webActionInjections\Mvc\Controller\AbstractActionController`

Testing
-------

[](#testing)

Unit test runnig from authentication module directory.

```
$ phpunit
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.9% 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

Every ~40 days

Total

2

Last Release

3859d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9d6b9ab1c535f3a27bd3b2bb97bc7d43c611cb69731861d2c2d60e1174b27ec4?d=identicon)[maxgu](/maintainers/maxgu)

---

Top Contributors

[![maxgu](https://avatars.githubusercontent.com/u/208688?v=4)](https://github.com/maxgu "maxgu (31 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")[![sebaks](https://avatars.githubusercontent.com/u/9897880?v=4)](https://github.com/sebaks "sebaks (1 commits)")

---

Tags

zf2action injectionsinjections

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/t4web-actioninjections/health.svg)

```
[![Health](https://phpackages.com/badges/t4web-actioninjections/health.svg)](https://phpackages.com/packages/t4web-actioninjections)
```

###  Alternatives

[zf-commons/zfc-base

A set of genetic (abstract) classes which are commonly used across multiple modules.

1441.1M25](/packages/zf-commons-zfc-base)[snapshotpl/zf-snap-geoip

MaxMind GeoIP Module for Zend Framework 2

1512.9k](/packages/snapshotpl-zf-snap-geoip)

PHPackages © 2026

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