如何从其他控制器获取部分页面


How to get part of the page from other controller

我的网站我有一些内容可以投票(+/-)。当所有内容都有自己的选民时,它现在运行得很好。现在我正在寻找一种方法来创建一个带有实体(votedModel、votedId、user、vote)的单个投票捆绑包。基本上捆绑包已经准备好了。我的问题是如何使用它。我希望能够做一些类似的事情:

class ... extends Controller {
    function showAction(Request $request,$id) {
        ...
        $voter=new Voter('myCOntentType',$id,$userid);
        ...
        return $this->render('...', array('voter'=>$voter->getVoter(),...))
    }
}

getVoter()将创建投票者视图。

  • 但我不知道该怎么开始。我试着用这种方式呼叫另一个控制器,但无法创建投票表。

  • 它与$voter=$this->forward('VoterbundleNewAction', array('id=>$id,'user'=>$user)->getContent();
    一起工作,但这不是我想的。

我认为我的做法完全错误,我可能需要这样做作为服务。我找不到路了。

您可以在您的twitch模板中使用include或render来获取其他模板的输出。因此,你可以创建一个模板(比如,vorter.html.titch),其中包含你的投票系统的html,在twig中,在任何需要投票人的地方,你都可以使用:

{% include "AcmeVoterBundle:Voter:voter.html.twig" %}

{% render "AcmeVoterBundle:Voter:voter" with {"item": item} %}

在第一个示例中,您只需包含另一个模板(另请参见:http://symfony.com/doc/current/book/templating.html#including-其他模板),在后一种情况下,您实际执行控制器的另一个操作方法,并将其输出放入当前模板中(另请参阅:http://symfony.com/doc/current/book/templating.html#embedding-控制器)