Symfony 在特定情况下重写模板和控制器
我有两个包A和B,在B中我需要重写A的模板,所以我使用了。 到目前为止,一切都很好,但现在我只需要在某些特定情况下覆盖bundle A的模板。我在bundle B的操作中尝试了这一点(它覆盖了bundle A的操作):Symfony 在特定情况下重写模板和控制器,symfony,overriding,bundle,Symfony,Overriding,Bundle,我有两个包A和B,在B中我需要重写A的模板,所以我使用了。 到目前为止,一切都很好,但现在我只需要在某些特定情况下覆盖bundle A的模板。我在bundle B的操作中尝试了这一点(它覆盖了bundle A的操作): 但无论如何,这都会覆盖,并且它会返回bundle B的模板,即使我的文章的类型是'general',只需执行以下操作: public function detailsAction(Request $request, $article) { if('general' ===
但无论如何,这都会覆盖,并且它会返回bundle B的模板,即使我的文章的类型是
'general'
,只需执行以下操作:
public function detailsAction(Request $request, $article)
{
if('general' === $article->getType()) {
// this doesn't return the template of the bundle A :(
return parent::detailsAction($request, $article);
}
// else go on on rendering the other template...
return $this->render('YourBundleA:YourControllerA:YourTemplateA.html.twig', array(...));
}
我发现了“bug”,我没有正确地设计整个覆盖的事情:因为我覆盖了A的控制器,覆盖树枝是完全没有用的,所以我重命名了捆绑包B的树枝。这样它就像一个符咒一样工作 这正是我所做的,但它呈现了B的模板,因为它与捆绑包A中的名称相同。我已经发布了我认为是一个好的解决方案
public function detailsAction(Request $request, $article)
{
if('general' === $article->getType()) {
// this doesn't return the template of the bundle A :(
return parent::detailsAction($request, $article);
}
// else go on on rendering the other template...
return $this->render('YourBundleA:YourControllerA:YourTemplateA.html.twig', array(...));
}