PHPackages                             biig/dictionary-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. biig/dictionary-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

biig/dictionary-bundle
======================

Are you often tired to repeat static choices like gender or civility in your apps ?

v2.1.7(6y ago)811.2k↓90.9%10[10 issues](https://github.com/biig-io/DictionaryBundle/issues)[1 PRs](https://github.com/biig-io/DictionaryBundle/pulls)MITPHPPHP &gt;=5.4

Since Nov 21Pushed 5y ago7 watchersCompare

[ Source](https://github.com/biig-io/DictionaryBundle)[ Packagist](https://packagist.org/packages/biig/dictionary-bundle)[ RSS](/packages/biig-dictionary-bundle/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (7)Dependencies (13)Versions (31)Used By (0)

DictionaryBundle
================

[](#dictionarybundle)

[![Build Status](https://camo.githubusercontent.com/4a544070cbff5f7792e226b86152d0a02132d4e2fb320001be177c1d4deaa837/68747470733a2f2f7472617669732d63692e6f72672f626969672d696f2f44696374696f6e61727942756e646c652e737667)](https://travis-ci.org/biig-io/DictionaryBundle)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/4576301fbddb9a4e9136e300ca0719659bddbc42ac5d5e6393de1f3b6ade562e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4b6e704c6162732f44696374696f6e61727942756e646c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/KnpLabs/DictionaryBundle/?branch=master)

Are you often tired to repeat static choices like gender or civility in your apps ?

**Notice: This bundle is a fork from KnpLabs/DictionaryBundle. We are working on releasing a 3.0 version but for now the released version is [2.x](https://github.com/biig-io/DictionaryBundle/tree/2.x). You should refere to the right branch to have a good documentation.**

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

[](#requirements)

- Symfony &gt;= 3.4
- PHP &gt;= 7.1

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

[](#installation)

Add the DictionaryBundle to your `composer.json`:

```
{
    "require": {
        "biig/dictionary-bundle": "^3.0"
    }
}
```

Or install it directly with composer command

```
composer require biig/dictionary-bundle
```

Register the bundle in `app/AppKernel.php`

```
$bundles = array(
    // ...
    new Knp\DictionaryBundle\KnpDictionaryBundle(),
);
```

Basic usage
-----------

[](#basic-usage)

Define dictionaries in your config.yaml file:

```
knp_dictionary:
    dictionaries:
        my_dictionary:      # your dictionary name
            - Foo           # your dictionary content
            - Bar
            - Baz
```

You will be able to retrieve it through the dictionary registry service:

```
$container->get('knp_dictionary.registry')->get('my_dictionary');
```

### Dictionary form type

[](#dictionary-form-type)

Now, use them in your forms:

```
use Knp\DictionaryBundle\Form\Type\DictionaryType;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('civility', DictionaryType::class, [
            'name' => 'my_dictionary'
        ])
    ;
}
```

The dictionary form type extends the [symfony's choice type](http://symfony.com/fr/doc/current/reference/forms/types/choice.html) and its options.

### Validation constraint

[](#validation-constraint)

You can also use the constraint for validation. The `value` have to be set.

```
use Knp\DictionaryBundle\Validator\Constraints\Dictionary;

class User
{
    /**
     * @ORM\Column
     * @Dictionary(name="my_dictionary", multiple=false)
     */
    private $civility;
}
```

Advanced usage
--------------

[](#advanced-usage)

You can specify the indexation mode of each dictionary

```
knp_dictionary:
    dictionaries:
        my_dictionary:                  # your dictionary name
            type: 'key_value'           # your dictionary type
            content:                    # your dictionary content
                "foo": "foo_value"
                "bar": "bar_value"
                "baz": "baz_value"
```

### Available types

[](#available-types)

- `value` (default) : Natural indexation
- `value_as_key`: Keys are defined from their value
- `key_value`: Define your own keys
- `callable`: Build a dictionary from a callable

### Callable dictionary

[](#callable-dictionary)

You can create a callable dictionary:

```
knp_dictionary:
    dictionaries:
        my_callable_dictionary:         # your dictionary name
            type: 'callable'            # your dictionary type
            service: 'app.service.id'   # a valid service from your application
            method: 'getSomething'      # the method name to execute
```

Callable dictionaries are loaded with a lazy strategy. It means that the callable will not be called if you do not use the dictionary.

### Categories in dictionaries

[](#categories-in-dictionaries)

If you want to retrieve some groups of dictionaries from the registry, then you can specify a category to your dictionary.

```
knp_dictionary:
    dictionaries:
        my_dictionary:                  # your dictionary name
            type: 'key_value'           # your dictionary type
            category: 'product.toy'     # your dictionary category
            content:                    # your dictionary content
                foo: foo_value
                bar: bar_value
                baz: baz_value
```

Get a registry for this category later:

```
// Retrieve all dictionaries of a special category
$dictionaries = $registry->filterByCategory('product.toy')->all();
```

Transformers
------------

[](#transformers)

For now, this bundle is only able to resolve your **class constants**:

```
my_dictionary:
    - MyClass::MY_CONSTANT
    - Foo
    - Bar
```

You want to add other kinds of transformations for your dictionary values ? Feel free to create your own transformer !

### Add your own transformers

[](#add-your-own-transformers)

Create your class that implements [TransformerInterface](src/Knp/DictionaryBundle/Dictionary/ValueTransformer/TransformerInterface.php). Load your transformer and tag it as `knp_dictionary.value_transformer`.

```
services:
    my_bundle.my_namespace.my_transformer:
    	class: %my_transformer_class%
    	tags:
        	- { name: knp_dictionary.value_transformer }
```

Use your dictionary in twig
---------------------------

[](#use-your-dictionary-in-twig)

You can also use your dictionary in your Twig templates via calling `dictionary` function (or filter)

```
{% for example in dictionary('examples') %}
    {{ example }}
{% endfor %}
```

But you can also access directly to a value by using the same function (or filter)

```
{{ 'my_key'|dictionary('dictionary_name') }}
```

Faker provider
--------------

[](#faker-provider)

The KnpDictionaryBundle comes with a [faker provider](https://github.com/fzaninotto/Faker) that can be used to provide a random entry from a dictionary.

### Alice

[](#alice)

To register the provider in [nelmio/alice](https://github.com/nelmio/alice), you can follow the [official documentation](https://github.com/nelmio/alice/blob/master/doc/customizing-data-generation.md#add-a-custom-faker-provider-class)

or ...

if you use the awesome [knplabs/rad-fixtures-load](https://github.com/knplabs/rad-fixtures-load) library, the dictionary provider will be automaticaly loaded for you :)

```
App\Entity\User:
    john_doe:
        firstname: John
        latnale: Doe
        city:
```

Use dictionary command
----------------------

[](#use-dictionary-command)

You can use the following command to show your app dictionaries easily:

```
php bin/console knp:dictionary:dump
```

If you want to display only one dictionary, you can set it name in argument

```
php bin/console knp:dictionary:dump my_dictionary
```

Create your own dictionary implementation
-----------------------------------------

[](#create-your-own-dictionary-implementation)

### Dictionary

[](#dictionary)

Your dictionary implementation must implements the interface [Dictionary](src/Knp/DictionaryBundle/Dictionary/Dictionary.php). In Symfony &gt;= 3.3, your class will be automatically register as dictionary service.

### Dictionary Factory

[](#dictionary-factory)

You must create a dictionary factory that will be responsible to instanciate your dictionary.

```
services:
    # Syntax Symfony >= 3.3
    App\Dictionary\Factory\MyCustomFactory:
        tags:
            { name: 'knp_dictionary.factory' }
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community23

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~76 days

Recently: every ~61 days

Total

25

Last Release

2399d ago

Major Versions

1.x-dev → v2.0.02017-12-12

2.1.2 → v3.0.0-alpha12018-07-06

v2.1.3 → v3.0.0-alpha22018-11-01

PHP version history (3 changes)v1.2.0PHP &gt;=5.3

v2.0.0PHP &gt;=5.4

v3.0.0-alpha1PHP ^7.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/132011?v=4)[Nikita (Anatolievich) Dudnik](/maintainers/Nek)[@Nek](https://github.com/Nek)

![](https://www.gravatar.com/avatar/3e67bc55b1325c1067ac18a70442c13dbe5caffe2e66e53154fd1f4499e86439?d=identicon)[athos7933](/maintainers/athos7933)

![](https://www.gravatar.com/avatar/d2ca28f6dac77e430669887ec9ded04b3ced584de539d50376c8f803f4c5786a?d=identicon)[babeou](/maintainers/babeou)

---

Top Contributors

[![Nek-](https://avatars.githubusercontent.com/u/972456?v=4)](https://github.com/Nek- "Nek- (44 commits)")[![PedroTroller](https://avatars.githubusercontent.com/u/1766827?v=4)](https://github.com/PedroTroller "PedroTroller (43 commits)")[![Shivoham](https://avatars.githubusercontent.com/u/1434539?v=4)](https://github.com/Shivoham "Shivoham (27 commits)")[![Awkan](https://avatars.githubusercontent.com/u/15607661?v=4)](https://github.com/Awkan "Awkan (16 commits)")[![akovalyov](https://avatars.githubusercontent.com/u/2339101?v=4)](https://github.com/akovalyov "akovalyov (10 commits)")[![babeou](https://avatars.githubusercontent.com/u/6522591?v=4)](https://github.com/babeou "babeou (2 commits)")[![mrburns](https://avatars.githubusercontent.com/u/159238?v=4)](https://github.com/mrburns "mrburns (1 commits)")[![Djeg](https://avatars.githubusercontent.com/u/1638230?v=4)](https://github.com/Djeg "Djeg (1 commits)")[![realPy](https://avatars.githubusercontent.com/u/7954984?v=4)](https://github.com/realPy "realPy (1 commits)")

---

Tags

dictionary

### Embed Badge

![Health badge](/badges/biig-dictionary-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/biig-dictionary-bundle/health.svg)](https://phpackages.com/packages/biig-dictionary-bundle)
```

###  Alternatives

[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k17.5M373](/packages/easycorp-easyadmin-bundle)

PHPackages © 2026

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