[英]Symfony2: How to get container parameters from parameters.yml in a twig filter/extension?
[英]Symfony: how to get values from parameters.yml in an AbstractType
我正在嘗試從我在parameters.yml中聲明的數組中獲取值
# parameters.yml
parameters:
...
objects: {object1: 1, object2: 2, object3: 3}
但是一旦我嘗試使用此代碼從該文件中獲取“對象”
$builder->add('list', ChoiceType::class, array ('objects' => $this->container->getParameter('categories') ));
我收到以下錯誤
Notice: Undefined property: Project\Bundle\Form\EntityType::$container
有什么建議么?
如果將Project\\Bundle\\Form\\EntityType
為service ,則可以將數據以及它需要的任何其他服務注入其中。 包含類型時,您仍然可以將其稱為EntityType::class
,因為Symfony將識別出該類已定義為服務。
它將使測試更加“有趣”
您必須將ContainerInterface注入類中。
services.yml
form.my_entity_type:
class: AppBundle\Form\MyEntityType
arguments:
- '@service_container'
tags:
- { name: form.type }
MyEntityType
/** @var ContainerInterface */
protected $container;
/**
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$params = $this->container->getParameter('my-params');
....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.