控制器必须在Symfony2中返回响应 [英] Controller must return a response in Symfony2

查看:96
本文介绍了控制器必须在Symfony2中返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个ajax调用.我想通过通话实现的效果很好.我想从数据库中删除一些记录,这些记录实际上是通过ajax调用该方法时删除的,但是就像在symfony方法中一样,它必须返回一个响应,这就是为什么在执行该方法时会给我错误

I have an ajax call in my code. What I want to achieve with the call works fine. I want to delete some records from the database which is actually deleted when the method is called via ajax but as in symfony method it must return a response and thats why when the method is executed its gives me the error

我的Ajax电话是

  $.ajax({
        type: "POST",
        data: data,
        url:"{{ path('v2_pm_patents_trashpatents') }}",
        cache: false,
        success: function(){
        document.location.reload(true);
        }
    });

执行的方法是

 public function trashpatentAction(Request $request){
    if ($request->isXmlHttpRequest()) {
        $id = $request->get("pid");
        $em = $this->getDoctrine()->getEntityManager();
        $patent_group = $em->getRepository('MunichInnovationGroupPatentBundle:PmPatentgroups')->find($id);
        if($patent_group){
            $patentgroup_id = $patent_group->getId();
            $em = $this->getDoctrine()->getEntityManager();
            $patents = $em->getRepository('MunichInnovationGroupPatentBundle:SvPatents')
            ->findBy(array('patentgroup' => $patentgroup_id));
            if($patents){
                foreach($patents as $patent){
                    if($patent->getIs_deleted()==false){
                        $patent->setIs_deleted(true);
                        $em->flush();
                    }
                }
            }
            $patent_group->setIs_deleted(true);
            $em->flush();
        }
        else{
            $em = $this->getDoctrine()->getEntityManager();
            $patent = $em->getRepository('MunichInnovationGroupPatentBundle:SvPatents')->find($id);
            if ($patent) {
                $patent->setIs_deleted(1);
                $em->flush();
            }
        }
        return true;
    }
}

如何从此方法成功返回? 有任何想法吗?谢谢

How can I successfully return from this method ? Any ideas? Thanks

推荐答案

return true;替换为return new Response();.同样不要忘记在顶部写use Symfony\Component\HttpFoundation\Response;.

Replace return true; with return new Response();. Also don't forget to write use Symfony\Component\HttpFoundation\Response; at the top.

这篇关于控制器必须在Symfony2中返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆