Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php FOSRestBundle:将其他变量传递给细枝模板_Php_Rest_Symfony_Fosrestbundle - Fatal编程技术网

Php FOSRestBundle:将其他变量传递给细枝模板

Php FOSRestBundle:将其他变量传递给细枝模板,php,rest,symfony,fosrestbundle,Php,Rest,Symfony,Fosrestbundle,我使用相同的操作来处理html和json响应,只是用.json作为后一个路由的后缀 json的工作没有问题,但是当响应是html时,我需要向Twig模板传递一个额外的变量,我不知道也不知道如何做。目前,我的代码是: // $entity = get my data; $name = 'foo'; $view = $this->view($entity, 200) ->setTemplate('MyBundle:MyController:myTemplate.html.twi

我使用相同的操作来处理html和json响应,只是用.json作为后一个路由的后缀

json的工作没有问题,但是当响应是html时,我需要向Twig模板传递一个额外的变量,我不知道也不知道如何做。目前,我的代码是:

// $entity = get my data;

$name = 'foo';

$view = $this->view($entity, 200)
   ->setTemplate('MyBundle:MyController:myTemplate.html.twig')
   ->setTemplateVar('entity')
   ;

return $this->handleView($view);
如何将$name传递给myTemplate.html.twig?我需要的是:

//...
->setTemplate('MyBundle:MyController:myTemplate.html.twig', array('name' => $name))
//...

好的,我得到了一个开头的回复

目前,没有直接的方法,但以下程序将起作用:

1在路由默认值中分别添加带有html和json值的_格式选项:

defaults: { _controller: myBundle:myController:myAction, _format: html } #or json
2添加$\格式作为操作签名的第一个参数:

public function myActionAction($_format, $any_other_paramer)
3有条件地向细枝模板添加更多变量:

if ($this->get('fos_rest.view_handler')->isFormatTemplating($_format)) {
  $view->setData(array('name' => $name, 'entity' => $view->getData()));
}