biig / dictionary-bundle
你是否经常在应用中重复使用静态选项,如性别或礼貌?
Requires
- php: ^7.1
- fzaninotto/faker: ^1.6
- symfony/config: ^3.4 || ^4.0
- symfony/dependency-injection: ^3.4 || ^4.0
- symfony/form: ^3.4 || ^4.0
- symfony/http-foundation: ^3.4 || ^4.0
- symfony/http-kernel: ^3.4 || ^4.0
- symfony/validator: ^3.4 || ^4.0
- twig/twig: ^2.0
Requires (Dev)
- bossa/phpspec2-expect: ^3.0
- friendsofphp/php-cs-fixer: ^2.11
- leanphp/phpspec-code-coverage: ^4.0
- phpspec/phpspec: ^4.0
- phpspec/prophecy: ^1.7
- phpunit/php-code-coverage: ^5.2.3
- sebastian/comparator: ^2.0
Suggests
- knplabs/rad-fixtures-load: Allows to autoregister the dictionary provider as an alice provider
- dev-master / 3.0.x-dev
- v3.0.0-alpha2
- v3.0.0-alpha1
- 2.x-dev
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- 2.1.2
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- 1.x-dev
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.0
- dev-feature/combined-dictionary
- dev-import-knp-features
- dev-feature/key_value_dictionary
- dev-feature/refactoring-dictionaries
- dev-feature/valuetransformer-refactoring
This package is auto-updated.
Last update: 2024-08-28 21:15:46 UTC
README
你是否经常在应用中重复使用静态选项,如性别或礼貌?
注意:此捆绑包是从 KnpLabs/DictionaryBundle 分支出来的。我们正在努力发布3.0版本,但目前的发布版本是 2.x。您应该参考正确的分支以获得良好的文档。
要求
- Symfony >= 3.4
- PHP >= 7.1
安装
将 DictionaryBundle 添加到您的 composer.json
{ "require": { "biig/dictionary-bundle": "^3.0" } }
或者使用 composer 命令直接安装
composer require biig/dictionary-bundle
在 app/AppKernel.php
中注册捆绑包
$bundles = array( // ... new Knp\DictionaryBundle\KnpDictionaryBundle(), );
基本用法
在您的 config.yaml 文件中定义词典
knp_dictionary: dictionaries: my_dictionary: # your dictionary name - Foo # your dictionary content - Bar - Baz
您可以通过词典注册服务检索它
$container->get('knp_dictionary.registry')->get('my_dictionary');
词典表单类型
现在,在您的表单中使用它们
use Knp\DictionaryBundle\Form\Type\DictionaryType; public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('civility', DictionaryType::class, [ 'name' => 'my_dictionary' ]) ; }
词典表单类型扩展了 symfony 的选择类型及其选项。
验证约束
您也可以使用约束进行验证。必须设置 value
。
use Knp\DictionaryBundle\Validator\Constraints\Dictionary; class User { /** * @ORM\Column * @Dictionary(name="my_dictionary", multiple=false) */ private $civility; }
高级用法
您可以指定每个词典的索引模式
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"
可用类型
value
(默认): 自然索引value_as_key
: 键由其值定义key_value
: 定义自己的键callable
: 从可调用对象构建词典
可调用词典
您可以创建可调用词典
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
可调用词典使用延迟策略加载。这意味着如果未使用词典,则不会调用可调用对象。
词典中的类别
如果您想从注册表中检索一些词典组,则可以为您的词典指定一个类别。
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
稍后获取此类别的注册表
// Retrieve all dictionaries of a special category $dictionaries = $registry->filterByCategory('product.toy')->all();
转换器
目前,此捆绑包只能解析您的 类常量
my_dictionary: - MyClass::MY_CONSTANT - Foo - Bar
您想为词典值添加其他类型的转换?请自由创建自己的转换器!
添加您的转换器
创建一个实现 TransformerInterface 的类。加载您的转换器并将其标记为 knp_dictionary.value_transformer
。
services: my_bundle.my_namespace.my_transformer: class: %my_transformer_class% tags: - { name: knp_dictionary.value_transformer }
在 twig 中使用您的词典
您也可以通过调用 dictionary
函数(或过滤器)在您的 Twig 模板中使用您的词典
{% for example in dictionary('examples') %} {{ example }} {% endfor %}
但您也可以使用相同的函数(或过滤器)直接访问值
{{ 'my_key'|dictionary('dictionary_name') }}
Faker 提供程序
KnpDictionaryBundle 附带一个 faker 提供程序,可用于从词典中提供随机条目。
Alice
要在 nelmio/alice 中注册提供程序,您可以按照 官方文档
或者 ...
如果您使用出色的 knplabs/rad-fixtures-load 库,词典提供程序将自动为您加载 :)
App\Entity\User: john_doe: firstname: John latnale: Doe city: <dictionary('cities')>
使用词典命令
您可以使用以下命令轻松显示您的应用程序词典
php bin/console knp:dictionary:dump
如果您只想显示一个词典,您可以在参数中设置其名称
php bin/console knp:dictionary:dump my_dictionary
创建您自己的词典实现
词典
您的字典实现必须实现Dictionary接口。在Symfony 3.3及以上版本中,您的类将自动注册为字典服务。
字典工厂
您必须创建一个字典工厂,该工厂将负责实例化您的字典。
services: # Syntax Symfony >= 3.3 App\Dictionary\Factory\MyCustomFactory: tags: { name: 'knp_dictionary.factory' }