dtdvbf37193 2017-02-02 20:46
浏览 40
已采纳

来自另一个控制器的Symfony Forwarding / Retriving结果不返回任何内容

I made two bundles. I have created an action method in each of their respective controllers.

I am trying to get result of A in B.

I have attempted to include the content of the view of what A generates in B but my include ends up missing a variable therefore I retrieve the result from forwarding it.

I read the documentation and decided to use $variable=$this->forwad(MyControllerA) but my $variable doesn't get any result when I read the data in the view.

I was wondering as I found no similar issue if I am doing something wrong or if I should do this differently.

Code

public function getAAction()
{
    $em = $this->getDoctrine()->getManager();

    $list_A = $em->getRepository('ABundle:A_Entity')->findAll();

    return $list_A;
}

public function getBAction()
{
    $em = $this->getDoctrine()->getManager();

    $list_B = $em->getRepository('PropertyBundle:B_Entity')->findAll();

    $list_A = $this->forward('A_Bundle:A_Entity:getA');

    return $this->render('@B/B/getB.html.twig', array('list_B' => $list_B, 'list_A' => $list_A));
}
  • 写回答

2条回答 默认 最新

  • dongluan7821 2017-02-04 18:56
    关注

    I found out i was wrong.

    I dumped the variable as suggested by @Rendy and did way deeper research to try and understand more. it averred that forward works with response.

    From there i tried manipulating the response i got but due to my lack of knowledge and despite the research i couldn't find how to retrieve the array i sent.

    Therefore i converted my controller into service as follow in services.yml of my bundle

        services:
       bundle_a:
           class: BundleA\Controller\AController
           calls:
                       - [ "setContainer", [ "@service_container" ] ]
    

    If you're wondering about the last line its because when calling the container is null. That line fixed it. For more info you can look at Symfony: Why is method $this->get() not available in constructor?

    Then i just called my method in the B Controller as follow

    $list_a= $this->get('bundle_a')->getAAction();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大