控制器必须返回响应

ike*_*rib 7 symfony

这是我的控制器:

    /**
 * Finds and displays a Formacion entity.
 *
 * @Route("/{id}/show", name="curso_show")
 * @Template()
 */
public function showAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $curso = $em->getRepository('GitekUdaBundle:Curso')->find($id);

    if (!$curso) {
        throw $this->createNotFoundException('Unable to find Curso entity.');
    }

    $deleteForm = $this->createDeleteForm($id);             

    // Detalle Formación
    $detcurso = new Detcurso();
    $formdetcurso   = $this->createForm(new DetcursoType(), $detcurso);

    return array(
        'curso'      => $curso,
        'delete_form' => $deleteForm->createView(),  
        'detcurso'      => $detcurso,
        'formdetcurso' => $formdetcurso,
        );
}
Run Code Online (Sandbox Code Playgroud)

在我的开发环境中工作正常(Mac),但当我去我的生产环境(CentOS服务器)我得到

The controller must return a response (Array(curso => Object(Gitek\UdaBundle\Entity\Curso), delete_form => Object(Symfony\Component\Form\FormView), detcurso => Object(Gitek\UdaBundle\Entity\Detcurso), formdetcurso => Object(Symfony\Component\Form\Form)) given).
Run Code Online (Sandbox Code Playgroud)

500内部服务器错误 - LogicException

任何线索?

ric*_*age 21

Symfony2期望从控制器操作返回Response对象.我猜你可能想要以下内容:

return $this->render(
    "YourBundlePath:Something:template.html.twig",
    array(
        'curso'        => $curso,
        'delete_form'  => $deleteForm->createView(),  
        'detcurso'     => $detcurso,
        'formdetcurso' => $formdetcurso,
    )
);
Run Code Online (Sandbox Code Playgroud)

$this->render()方法将呈现提供的模板名称,并在上面的示例中,将模板传递给您的参数数组.它会将这个生成的内容包装在一个Response对象中,这正是Symfony2所期望的.

您还可以直接返回新的Response对象,例如,return new Response('Hello, world')如果需要.

有关详细信息,请参阅文档.

  • 这不是一个真正的解决方案,而是一种解决方法.@Koc在他的回答中遇到了真正的问题. (9认同)

小智 5

好吧我也遇到了这个问题,这篇帖子离我的问题最近,但没有解决,所以我想在这里写一下我的解决方案以防万一有人也有我的问题.

似乎模板注释存在问题,就像上面有人说的那样,你可以使用渲染函数来解决问题,但我认为这不是一个好的解决方案,因为@template注释试图使这个模板管理更容易并被使用symfony crud生成器中的默认值.

所以,我的问题是我安装了FosRestBundle,并且在安装它时我必须通过将这些行添加到config.yml来禁用模板注释:

#The view_annotations flag must be false to work with FOSRestBundle
sensio_framework_extra:
    view:
        annotations: false   
Run Code Online (Sandbox Code Playgroud)

您必须删除这些行才能使@template注释再次起作用.这很奇怪,因为我在完成最后一个作曲家更新之后才遇到这个问题,但之前没有.

这是遵循捆绑安装文档的问题,而不了解你在做什么,呵呵^ _ ^!

希望这有助于某人