byscripts / static-entity-bundle
为StaticEntity添加Symfony3和Symfony4的ParamConverter和FormType功能
Requires
- php: >=5.3.3
- byscripts/static-entity: ~2.0
- sensio/framework-extra-bundle: ~3.0
- symfony/config: ~3.0|^4.0
- symfony/dependency-injection: ~3.0|^4.0
- symfony/form: ~3.0|^4.0
- symfony/http-foundation: ~3.0|^4.0
- symfony/http-kernel: ~3.0|^4.0
- symfony/options-resolver: ~3.0|^4.0
Requires (Dev)
- symfony/browser-kit: ~3.0|^4.0
- symfony/expression-language: ~3.0|^4.0
- symfony/finder: ~3.0|^4.0
- symfony/templating: ~3.0|^4.0
- symfony/twig-bundle: ~3.0|^4.0
- symfony/validator: ~3.0|^4.0
- symfony/yaml: ~3.0|^4.0
This package is auto-updated.
Last update: 2024-09-30 01:35:53 UTC
README
此包为StaticEntity库提供对Symfony 3和Symfony 4的ParamConverter和FormType支持。
如果您只需要使用StaticEntity,而不需要FormType和ParamConverter支持,则不需要此包。
您可以将StaticEntity用于项目,就像使用其他库一样。
此4.x分支支持Symfony 3和Symfony 4
使用3.x分支支持SymfonyFrameworkBundle 3.x(由Symfony 2.4+使用)
对于SensioFrameworkBundle 2.x的支持(由Symfony 2.3使用),请使用1.x分支
安装
在composer.json中添加包
在命令行中运行composer require byscripts/static-entity-bundle:~4.0
启用包
Symfony 3
将Byscripts\Bundle\StaticEntityBundle\ByscriptsStaticEntityBundle
添加到app/AppKernel.php
文件中。
Symfony 4
将Byscripts\Bundle\StaticEntityBundle\ByscriptsStaticEntityBundle::class => ['all' => true]
添加到config/bundles.php
文件中。
使用方法
创建静态实体
首先,在您的项目中创建一个静态实体(例如,在src/AppBundle/StaticEntity
中)
<?php namespace AppBundle\StaticEntity; use Byscripts\StaticEntity\StaticEntity; class Civility extends StaticEntity { private $name; private $shortName; public static function getDataSet() { return array( 'mr' => array('name' => 'Mister', 'shortName' => 'Mr'), 'mrs' => array('name' => 'Misses', 'shortName' => 'Mrs'), ); } }
有关StaticEntity使用的更多详细信息,请参阅StaticEntity README.md
使用FormType
$builder->add('civility', StaticEntityType, $parameters);
参数列表
| 类 | 必需 | 静态实体的FQCN | null | | 函数 | 可选 | 使用获取SE实例的函数 | getAll |
StaticEntityType
扩展了本机ChoiceType
,可以使用其任何选项。
使用ParamConverter
class MyController { public function myAction(Civility $civility) { $name = $civility->getName(); } }