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

时间:2017-02-02 20:46:50

标签: php symfony

我做了两个捆绑。我已经在每个控制器中创建了一个动作方法。

我想在B中得到A的结果。

我试图包含A在B中生成的视图的内容,但是我的include最终错过了一个变量,因此我通过转发它来检索结果。

我阅读了文档,并决定使用$variable=$this->forwad(MyControllerA)但我的$variable在阅读视图中的数据时没有得到任何结果。

我很想知道如果我做错了什么或者我应该采取不同的做法,我发现没有类似的问题。

代码

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 个答案:

答案 0 :(得分:0)

应该是application.properties而不是$variable=$this->forwad(MyBundle:MyControllerA:myAction)

http://symfony.com/doc/current/controller/forwarding.html

向我们展示代码,以便您获得更好的帮助

答案 1 :(得分:0)

我发现我错了。

我按照@Rendy的建议抛弃了变量,并进行了更深入的研究,试图了解更多。它认为前锋与回应有关。

从那里我尝试操纵我得到的反应,但由于我缺乏知识,尽管研究我无法找到如何检索我发送的数组。

因此我将我的控制器转换为服务,如我的捆绑包的services.yml中所示

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

如果你想知道它的最后一行是因为当调用容器时为null。那条线修好了。 有关详细信息,请查看Symfony: Why is method $this->get() not available in constructor?

然后我在B Controller中调用了我的方法,如下所示

$list_a= $this->get('bundle_a')->getAAction();