PHPackages                             diepartments-pimcore-plugin/dependency-injection - 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. diepartments-pimcore-plugin/dependency-injection

ActivePimcore-plugin

diepartments-pimcore-plugin/dependency-injection
================================================

A Pimcore plugin enabling dependency injection

21.8kPHP

Since Aug 31Pushed 9y ago2 watchersCompare

[ Source](https://github.com/diePartments/pimcore-dependency-injection-plugin)[ Packagist](https://packagist.org/packages/diepartments-pimcore-plugin/dependency-injection)[ RSS](/packages/diepartments-pimcore-plugin-dependency-injection/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Pimcore Dependency Injection Plugin
===================================

[](#pimcore-dependency-injection-plugin)

Plugin for using the [dependency injection software design pattern](https://en.wikipedia.org/wiki/Dependency_injection) in [Pimcore](https://www.pimcore.org/) projects. It uses the amazing [PHP-DI](http://php-di.org/) dependency injection container and enables you to use all of its injection options.

Basic Usage
-----------

[](#basic-usage)

```
class SomeController extends Action
{
    /**
     * This service object gets injected automatically without any further config.
     * Important thing is that you add the @Inject annotation as well as the objects type (class)
     *
     * @Inject
     * @var \Website\Service\SomeService
     */
    private $someService;

    public function indexAction()
    {
        // just use the service anywhere in your controller
        $this->view->some = $this->someService->doSomething();
    }
}
```

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

[](#installation)

Add the following line to the `require` section of your composer.json

```
"diepartments-pimcore-plugin/dependency-injection": "dev-master"

```

Run `composer install` from your commandline and enable the plugin in the Pimcore Extension Manager afterwards.

For more information about how to use extensions via composer, [take a look at the documentation](https://www.pimcore.org/wiki/display/PIMCORE4/Extension+management+using+Composer)

Advanced Usage
--------------

[](#advanced-usage)

You can use all injection options provided by PHP-DI. The Plugins installer creates all necessary configuration files for you. They are located at `/website/var/plugins/dependency-injection`

Put your service configurations in `container.php` and your parameters in `parameters.php`.

You can also create environment specific definitions, to override defaults. Simply create a `container.ENV.php ` file and put your env specific services configuration in it. *Replace `ENV` by the environment you want to create specific config for, e.g. `development`*

The [Pimcore System environment](https://www.pimcore.org/wiki/pages/viewpage.action?pageId=20217900).

### Example

[](#example)

Lets configure a shopping cart. The cart should use an external system, like an ERP, to calculate shipping costs or customer specific prices. Therefore it needs a Gateway which knows how to communicate with the external System. The Gateway itself needs to know the api path its calling.

#### Example container.php

[](#example-containerphp)

```
