API Json symfony 形式

API Json in symfony form

我正在 symfony 2.8(使用 php5.4)创建一个应用程序,并且在我的表单中(我正在构建)我想通过 [=18= 显示项目列表] json 格式。 现在我卡住了,我不知道该怎么做。 我知道 API 的数据库有一个 table "projects" 并且我希望目标列 'name' 显示项目名称 这是我的代码:

/**
* @Route("/form")
*/
public function formAction(Request $request)
{

  $url = 'https://website.com/projects.json';
  $get_json = file_get_contents($url);
  $json = json_decode($get_json);

  $form = $this->createFormBuilder()
    ->add('Project', 'choice') // <-- ???
    ->add('send', 'submit' ,array('label' => 'Envoyer'))
    ->getForm();
  $form->handleRequest($request);

  return $this->render('StatBundle:Default:form.html.twig', array('form' =>  $form->createView(), 'project' => $json));

}

您可以将选择作为第二个参数作为数组传递,例如:

$jsonAsArray = json_decode($get_json, true); // with true return an array

$builder->add('Project', 'choice', array(
    'choices'  => $jsonAsArray,
    // *this line is important, depends of the data*
    'choices_as_values' => false,
));

更多信息in the doc here

希望对您有所帮助