如何多次扩展捆绑包?
在我的 Symfony2 应用程序中,我有一个名为 AnimalsBundle()
的非常基本的包,其中包含一个非常基本的实体。
我可以通过 捆绑继承。但是,无法再注册一个同样扩展 AnimalsBundle()
的包 InsectsBundle()
。每当我尝试这样做时,Symfony 就会抛出一个
[逻辑异常] 捆绑包“AnimalsTextBundle”直接由两个捆绑包“MammalsBundle”和“InsectsBundle”扩展。
所以开箱即用显然是不允许的。首先,我不太确定为什么这是不允许的,而且最重要的是我该如何解决这个问题?
In my Symfony2 app I'm having a very basic bundle named AnimalsBundle()
with a very basic entity.
I can successfully extend this bundle by creating a new bundle MammalsBundle()
via Bundle Inheritance. However, it is not possible to register one further bundle InsectsBundle()
that also extends the AnimalsBundle()
. Whenever I'm trying to do this, Symfony throws a
[LogicException]
Bundle "AnimalsTextBundle" is directly extended by two bundles "MammalsBundle" and "InsectsBundle".
So out of the box it's obviously not allowed. First of all, I'm not really sure why this is not allowed and - most important - how can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道已经过去一年多了,但我刚刚遇到了你的问题,答案可能对某人有用。
Symfony 不允许一个包直接由多个包扩展,因为如果两个包是压倒性的相同的文件,将无法确定应使用哪个包。但是,您可以通过执行以下操作来实现您想要的目的:
AnimalsBundle
<|----MammalsBundle
<|-----InsectsBundle
>这样 InsectsBundle 间接将 AnimalBundle 作为父级,并且可以覆盖其中的文件。
I know it's been more than year now, but I just came across your question and the answer maybe useful to someone anyway..
Symfony doesn't allow a bundle to be extended directly by more than one bundle, simply because if two bundles are overriding the same files, it wouldn't be possible to determine what bundle should be used. However you can achieve what you want by doing the following :
AnimalsBundle
<|----MammalsBundle
<|-----InsectsBundle
This way InsectsBundle indirectly has AnimalBundle as a parent and can override files from it.
我知道,自从提出问题以来,这已经是一个日志时间了,但是对于那些需要答案的人,我会建议以下内容:
1)尝试避免捆绑继承,除非您 100% 肯定需要这种情况到。
2) 鉴于所讨论的示例,更好的设置将如下所示:您有一个
CreatureBundle
,它主要由抽象类和接口组成。使每个后代包依赖于CreatureBundle
并使用CreatureBundle
中的抽象类和接口实现每个Creature
特定代码。根据个人经验,我可以看出,在出现问题时,管理依赖项比管理继承问题要容易得多。如果您需要更改祖先包的逻辑,您将节省大量时间,因为不必挖掘继承的代码并更改每个后代包中的相同逻辑。
编辑:虽然我的建议可能会导致更紧密的耦合,并且基本上与最新的 Symfony 的“最佳实践”指南相矛盾(该指南指出“每个应用程序单个捆绑”是最佳实践),但最终您会意识到这种方法最终使代码维护变得更容易。
I know, that this has been a log time since the question has been asked, but for those in need of an answer I'll suggest the following:
1) Try to avoid bundle inheritance except the cases you are 100% positive that you need to.
2) Given the example in question, the better setup will look smth like this: you have a
CreatureBundle
, which consists mostly of abstract classes and interfaces. Make each descendant bundle depend onCreatureBundle
and implement eachCreature
specific code with those abstract classes and interfaces inCreatureBundle
.Based on personal experience I can tell that managing dependencies is much easier task than managing inheritance issues in case something goes wrong. If you'll ever need to alter the ancestor bundle's logic, you'll save yourself lot time by not having to dig through inherited code and alter the same logic in every descendant bundle.
Edit: Although my suggestions might lead to tighter coupling and basically contradicts latest Symfony's 'best practices' guide (which states that 'single bundle per app' is a best practice), in the end you'll realize that this approach eventually makes code maintenance easier.
我想不出您可能需要这样做的用例。捆绑包几乎就像独立的应用程序一样。如果您需要来自另一个包的资源,您可以使用依赖注入容器。
也许您应该重新考虑您的项目结构。
I can't think of a use case where you could possibly need to do this. Bundles are meant to be almost like standalone applications. You have the dependency injection container at your disposal if you need resources from another bundle.
Perhaps you should re-think your project structure.