从控制器中检索HTML代码

时间:2012-08-18 11:49:21

标签: symfony

是否可以检索控制器B 控制器A 生成的html代码?

控制器A

/**
 *
 *
 * @Route("/{user_id}/cart", name="user_cart")
 * @Template()
 */
public function showCartAction($user_id)
{

    $cart = $this->getCartManager()
        ->getUserCart($user_id);

    return array(
        'cart'=> cart
    );

}

控制器B

/**
 *
 *
 * @Route("/html", name="htmlGenerated")
 * @Template()
 */
public function showHTMLAction()
{
    $user_id = 3;

    $html = //How to obtain the html generated by Controller A with UserId = 3 ????
//...

}

1 个答案:

答案 0 :(得分:2)

你可以forward控制器B中的请求

public function showHTMLAction()
{
    $user_id = 3;

    $html = $this->forward('AcmeDemoBundle:ControllerB:showCardAction', array(
        'user_id' => $user_id,
    ))->getContent();        
}

尽管这应该可以完美地运行,但实际上我会建议您在模板中使用embed the controller