【问题标题】:overriding template and controller in specific cases在特定情况下覆盖模板和控制器
【发布时间】:2014-10-08 07:29:26
【问题描述】:

我有两个包 A 和 B,在 B 中我需要覆盖 A 的模板,所以我使用了bundle inheritence。 到目前为止一切正常,但现在我只需要在某些特定情况下覆盖包 A 的模板。我在包 B 的操作中尝试了这个(它覆盖了包 A 的操作):

class ArticleController extends BaseArticleController
{
   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...
   }

但这无论如何都会覆盖,即使我的文章的类型是'general',它也会返回包 B 的模板

【问题讨论】:

    标签: symfony overriding bundle


    【解决方案1】:

    只需这样做:

    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(...));
    }
    

    【讨论】:

    • 这正是我所做的,但它正在渲染 B 的模板,因为它与包 A 中的名称相同。我已经发布了我认为这是一个很好的解决方案
    【解决方案2】:

    我发现了“错误”,我没有正确设计整个覆盖的东西:因为我覆盖了 A 的控制器,覆盖树枝是完全没用的,所以我重命名了捆绑包 B 的树枝. 这样它就像一个魅力!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2018-06-14
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      相关资源
      最近更新 更多