Symfony2: How to extend a Bundle?

纵然是瞬间 提交于 2019-12-12 06:17:20

问题


If i have AlphaBunde that is parent of BetaBundle how can i extend a twig block without overriding the entire template?

How can i import the routes that exist only in BetaBundle?


回答1:


You cannot extend a block in Twig. You can overload it if you extend a template (and call parent() which kind of works as inheritance).

If you extend bundle you can overload its controllers or resources. I don't think you can really extend a template from a parent bundle because of how the paths are resolved. You can read more about it in the Extending a Bundle documentation chapter.

Also, How to use Bundle Inheritance to Override parts of a Bundle might clarify few things.

Remember to check Overriding Bundle Templates to learn how to overload templates in an application.

About routes: I think you'll have to define each route in your application's configuration file if you want to import them selectively.




回答2:


If you want to use template from another bundle and you do not want to override whole template you use this:

// Your file in ProjectAplhaBundle index.html.twig

{% extends "ProjectBetaBundle::layout.html.twig" %}

{% block content %}
    {{ parent() }}
    Somethings added to the existing content    
{% endblock %}

For routing from just BetaBundle remove all routes (routing.yml in app folder) and leave just one with something like this:

ProjectBetaBundle:
    resource: "@ProjectBetaBundle/Resources/config/routing.yml"
    prefix:   /

Then you specify all your routes in @ProjectBetaBundle/Resources/config/routing.yml

I hope that helps. Cheers



来源:https://stackoverflow.com/questions/9021367/symfony2-how-to-extend-a-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!