PHPackages                             seiffert/helper-bundle - 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. seiffert/helper-bundle

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

seiffert/helper-bundle
======================

This bundle provides a structure for helper objects.

01521PHP

Since Mar 15Pushed 13y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (1)

SeiffertHelperBundle [![Build Status](https://camo.githubusercontent.com/2773885a07745a2f6a1e9255d056778b7529cbad32be5572c957ee0eb6fa63f6/68747470733a2f2f7472617669732d63692e6f72672f73656966666572742f68656c7065722d62756e646c652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/seiffert/helper-bundle)
======================================================================================================================================================================================================================================================================================================================

[](#seifferthelperbundle-)

This bundle introduces a structure for helper objects.

Concepts
--------

[](#concepts)

After developing Symfony2 applications for quite some time, I stumbled upon different use cases for some kind of helper objects. One of them for instance are controllers: As soon as you stop to use `FrameworkBundle`'s default controller as base class for your controllers, you probably start writing code that replaces the helper functions in the default controller. This code will probably be required in most of your controllers. In this situation, you either create controller classes that all have a lot of dependencies that are injected by the DIC, you re-introduce a base controller class, or you employ some kind of helper objects.

This bundle solves this kind of situations by introducing helper objects that are managed by the dependency injection container (thus they are created lazily and will be reused) and are made available via a single helper object - the `HelperBroker`. One broker aggregates multiple helpers that are collected and configured during container compilation and proxies calls to helper methods to the actual helper objects.

Setup
-----

[](#setup)

Require the package via composer:

`composer.json`:

```
    "require": {
        ...
        "seiffert/helper-bundle": "*",
        ...
    }

```

Activate the bundle in your AppKernel:

`app/AppKernel.php`:

```
    public function registerBundles()
    {
        $bundles = array(
            ...
            new Seiffert\HelperBundle\SeiffertHelperBundle(),
            ...
        );
        ...
    }

```

Usage
-----

[](#usage)

There are different ways of using helpers and helper brokers. In the following, I will explain the two, which appear most useful to me.

### via tagged service

[](#via-tagged-service)

To configure helpers as services, you just implement your custom helper classes, add definitions for them to the DIC, and tag them like this:

```
services:
    my.helper:
        class: My\Helper\MyHelper
        tags:
            - { name: seiffert.helper, broker: my.helper.broker }
    my.second.helper:
        class: My\Helper\MySecondHelper
        tags:
            - { name: seiffert.helper, broker: my.helper.broker }
    my.service.that.requires.help:
        class: My\Service\ThatRequiresHelpService
        arguments:
            - @my.helper.broker

```

The argument injected into the class `My\Service\ThatRequiresHelpService` is of type `Seiffert\HelperBundle\HelperBroker` and answers to all methods defined in your helper classes `My\Helper\MySecondHelper` and `My\Helper\MyHelper`. This is done by proxying these method calls to the actual helper classes using `__call`. To further illustrate the usage of a helper broker using the same scenario as above, I will sketch out the mentioned classes:

**`My\Helper\MyHelper`:**

```
