Installation:
composer require biig/dictionary-bundle
Ensure your AppKernel.php includes:
new Knp\DictionaryBundle\KnpDictionaryBundle(),
Define a Dictionary:
Add a dictionary to config/packages/knp_dictionary.yaml:
knp_dictionary:
dictionaries:
gender:
- { value: 'male', label: 'Male' }
- { value: 'female', label: 'Female' }
- { value: 'other', label: 'Other' }
First Use Case: Retrieve the dictionary in a controller/service:
use Knp\DictionaryBundle\Registry\DictionaryRegistryInterface;
public function __construct(DictionaryRegistryInterface $dictionaryRegistry) {
$this->dictionaryRegistry = $dictionaryRegistry;
}
public function someAction() {
$genderDict = $this->dictionaryRegistry->get('gender');
$maleLabel = $genderDict->getLabel('male'); // Returns 'Male'
}
Dictionary Definition:
gender, status).Knp\DictionaryBundle\Dictionary\AbstractDictionary and register via services.Integration with Forms:
use Knp\DictionaryBundle\Form\Type\DictionaryType;
$builder->add('status', DictionaryType::class, [
'dictionary' => 'order_status',
'expanded' => true,
'multiple' => false,
]);
Translation Support:
Define labels in translation files (e.g., translations/messages.en.yaml):
gender:
male: 'Mr.'
female: 'Ms.'
other: 'Other'
Then reference in YAML:
gender:
- { value: 'male', label: 'gender.male' }
Dependency Injection:
Inject DictionaryRegistryInterface into services/controllers to reuse dictionaries across the app.
Dynamic Dictionaries: Create a custom dictionary service and register it programmatically:
services:
app.custom_dictionary:
class: App\Dictionary\CustomDictionary
tags: ['knp_dictionary.dictionary']
Validation Constraints: Use dictionaries in validation rules:
use Knp\DictionaryBundle\Validator\Constraints\Dictionary;
/**
* @Assert\IsTrue(message="Invalid status.")
* @Assert\Dictionary(dictionary="order_status")
*/
private $status;
Branch Confusion:
The package is actively maintained on the 3.x branch (not the default master). Ensure you reference the correct branch in docs/installation.
YAML Parsing Quirks:
'male' not male).label: 'gender.male').order-status may cause issues; use order_status instead).Caching:
Dictionaries are cached by default. Clear the cache (php bin/console cache:clear) after changes to config/knp_dictionary.yaml.
Symfony 5+ Compatibility:
The bundle is designed for Symfony 3.4+. For Symfony 5+, ensure AppKernel.php is updated to config/bundles.php if migrating.
$this->dictionaryRegistry->getAvailableDictionaries(); // Lists all loaded dictionaries.
php bin/console debug:config knp_dictionary to verify dictionary structure.Custom Dictionary Classes:
Extend AbstractDictionary to add logic (e.g., database-backed dictionaries):
class DbDictionary extends AbstractDictionary {
public function getItems() {
return $this->entityManager->getRepository(MyEntity::class)->findAll();
}
}
Override Default Templates:
The bundle uses Twig templates for rendering (e.g., dictionary_select.html.twig). Override them in your theme.
Event Listeners:
Listen to knp_dictionary.dictionary.load to modify dictionaries at runtime:
services:
app.dictionary_listener:
class: App\EventListener\DictionaryListener
tags:
- { name: kernel.event_listener, event: knp_dictionary.dictionary.load, method: onLoad }
How can I help you explore Laravel packages today?