0

我有一个JSON包含所有世界语言的文件,并希望将它们放入ChoiceType表单字段内的选择数组中。

$builder->add('languages', ChoiceType::class, array(
    'choices' => array()
))

我的JSON文件被存储:projectname/web/bundles/index/json/languages.json

JS不写/就可以实现AJAX吗?

PS EventListeners或其他Symfony2适合我的替代品。

4

1 回答 1

1

您可以使用 DIR 访问该文件,因为我不知道带有 builder 的 php 文件的位置,它可能如下所示:

 $builder->add('languages', ChoiceType::class, array(
        'choices' => json_decode(
            //if builder is in controller, this should work
            file_get_contents(__DIR__.'/../../../web/bundles/index/json/languages.json'),
            true)
    ));
于 2016-12-21T10:14:20.990 回答