簡體   English   中英

從另一個控制器獲取Symfony轉發/檢索結果不返回任何內容

[英]Symfony Forwarding / Retriving result from another controller returns nothing

我做了兩捆。 我已經在各自的控制器中創建了一個動作方法。

我正在嘗試在B中獲得A的結果。

我試圖在B中包含A生成的視圖的內容,但我的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));
}

應該是$variable=$this->forwad(MyBundle:MyControllerA:myAction)而不是$variable=$this->forwad(MyControllerA)

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

向我們顯示代碼,以便您獲得更好的幫助

我發現我錯了。

我按照@Rendy的建議轉儲了變量,並進行了更深入的研究,以嘗試進一步了解。 平均而言,前鋒能有效應對。

從那里我嘗試操縱我收到的響應,但是由於我缺乏知識,盡管進行了研究,但是我找不到如何檢索我發送的數組。

因此,我將我的控制器轉換為服務,如下所示在我的捆綁包的services.yml

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

如果您想知道最后一行,因為在調用容器時為null。 那條線固定了它。 有關更多信息,請參見Symfony:為什么$ this-> get()方法在構造函數中不可用?

然后我只是在B Controller按如下方式調用了我的方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM