如何与多个项目共享 Symfony2 模型
我们正在创建一个监控某些资产的 SaaS。这意味着它接收数据、保存数据并将其显示在网络界面中。
为此,我们使用/正在迁移到 Symfony2 创建了一些组件:
- 前端 Web 应用程序,用户可以在其中查看其数据
- 后端管理 Web 应用程序,我们在其中创建新的监视器、用户等
- 一个 API
- 一个应用程序从队列中检索接收到的数据并将其放入我们的数据库中(现在这是一个单独的脚本,但我正在考虑将其重新设计为由 cron 调用的 Symfony 命令)
所有这四个应用程序共享相同的模型:我们的主数据库保存所有用户、监视器和数据。
我的问题是:我应该如何在 Symfony2 中构建这些项目?
- 我是否创建一个单独的包来保存我的数据库的实体,并使四个项目包含这些实体并使用它们?
- 我可以在我的 Symfony 应用程序文件夹中创建一个“model”目录,以供我的 /src 目录中的所有包使用吗?
- 还有其他更干净的方法吗?
选项 1 似乎有点奇怪,因为根据我的理解,捆绑包需要路由、视图、控制器等。仅将它用于实体会有点奇怪。
选项 2 似乎没问题,因为 /app 文件夹对于 /src 文件夹中的所有内容都被视为“公用”(例如,因为参数也驻留在此处)。但是,那里没有“model”文件夹,我不确定应该有吗?
据我所知,Symfony 2 的“最佳实践”很少,因为它是全新的。但我想看看您认为是否有比其他做法更可取的做法。
任何反馈都非常受欢迎。 预先感谢,
迪特
We are creating a SaaS that monitors certain assets. This means it takes in data, saves it, and displays it in a webinterface.
For this, we have a few components that we created with/are moving to Symfony2:
- a frontend web application, where users can view their data
- a backend administrative web application, where we create new monitors, users, etc.
- an API
- an application that retrieves the received data from a queue and puts it in our database (this is now a seperate script, but I'm thinking of reworking this as a Symfony command that is called by cron)
All these four applications share the same model: our main database that holds all the users, monitors, and data.
My question is: how should I structure these projects in Symfony2?
- Do I create a seperate bundle which holds the entities for my database, and have the four projects include those entities and work with them?
- Can I make a 'model' directory in my Symfony app folder, which is used by all the bundles in my /src directory?
- Some other, cleaner way to do this?
Option 1 seems a bit weird, since a bundle, to my understanding, needs routing, views, controllers, etc. Using it for just entities would be a bit weird.
Option 2 seems alright, since the /app folder is considered 'communal' anyway for everything that is in the /src folder (since, for example, parameters reside there as well). However, there is no 'model' folder there, and I'm not sure that there should be?
I understand that there are very few 'best practices' out already for Symfony 2, since it's brand new. But I wanted to see if there are any practices more preferable then others, in your opinion.
Any feedback is more then welcome.
Thanks in advance,
Dieter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我目前正在做的是第一个选项:为您的实体创建一个单独的包。
这是我存储装置、实体、表单和实体相关测试的地方。
捆绑包不需要需要路由、控制器、视图等。我实际上见过蓝图捆绑包,它所做的只是附带 blueprint-css 资源,以便可以在项目中轻松重用它们。
至于将模型添加到应用程序目录......我不喜欢那样。我认为应用程序目录是所有配置应该存放的地方。尽管您可以覆盖
app/Resources
下的视图,但每当我想要覆盖某些内容时,我都会创建一个新包。What I am currently doing is the first option: create a separate bundle for your entities.
That's where I store fixtures, entities, forms and entity-related tests.
A bundle does NOT need to have routing, controllers, views etc. I've actually seen a blueprint bundle, and all it does is ship blueprint-css resources with it so they can be easily reused in projects.
As for adding models to the app directory... I wouldn't like that. I see the app directory as a place where all the configuration should be. Even though you can override views under
app/Resources
, whenever I want to override something I create a new bundle.我自己没有在现实世界的 symfony2 应用程序中使用过这种技术 - 但它是一个指针,因为您要求任何反馈。
服务容器 似乎是 Smyfony2 使服务在全球范围内可用的方法。因此,在您的情况下,模型访问对象将被定义为提供的链接中讨论的服务,然后可以从任何捆绑包中使用。
现在服务对象位于哪个包中?我们可以将它们放入单独的包中,因为它们在其他包之间共享。但是,我假设模型对于所有捆绑包来说不会完全对称,因此我们可以将共享模型放入单独的捆绑包中,
并将特定于捆绑包的实体放入捆绑包本身中。然后,可以使用上面链接中讨论的注入技术来提供特定于每个捆绑包的完整模型。
这似乎提供了最大程度的解耦。
我也对对此的任何反馈感兴趣,因为这是常见的设计场景。
问候。
I haven't used this technique myself in a real world symfony2 app - but it's a pointer as you asked for any feedback.
The service container seems to be the Smyfony2 method to make services available globally. So in your case the model access objects will be defined as services as discussed in the provided link, and then can be used from any bundle.
Now in which bundle do the service objects go? We can put them into a separate bundle as they are shared among other bundles. However, I assume the model would not be perfectly symmetrical for all the bundles, so we can put the shared model into a separate bundle,
and put bundle specific entities into the bundle itself. Then injection techniques discussed in the link above can be used to provide a full model specific to each bundle.
This seems to provide maximum de-coupling.
I'm interested in any feedback on this too as it's a common design scenario.
Regards.