简体   繁体   English

如何与几个项目共享Symfony2模型

[英]How to share a Symfony2 model with several projects

We are creating a SaaS that monitors certain assets. 我们正在创建一个监控某些资产的SaaS。 This means it takes in data, saves it, and displays it in a webinterface. 这意味着它接收数据,保存数据并将其显示在Web界面中。

For this, we have a few components that we created with/are moving to Symfony2: 为此,我们使用/创建的一些组件正在转移到Symfony2:

  • a frontend web application, where users can view their data 一个前端Web应用程序,用户可以在其中查看其数据
  • a backend administrative web application, where we create new monitors, users, etc. 后端管理Web应用程序,我们在其中创建新的监视器,用户等。
  • an API 一个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) 一个应用程序,它从队列中检索接收的数据并将其放入我们的数据库中(这是一个单独的脚本,但我正在考虑将其重新编写为由cron调用的Symfony命令)

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? 我的问题是: 我应该如何在Symfony2中构建这些项目

  1. 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? 我是否创建了一个单独的包来保存我的数据库的实体,并让这四个项目包含这些实体并使用它们?
  2. Can I make a 'model' directory in my Symfony app folder, which is used by all the bundles in my /src directory? 我可以在我的Symfony app文件夹中创建一个'model'目录,我的/ src目录中的所有bundle都使用它吗?
  3. 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. 选项1看起来有点奇怪,因为根据我的理解,捆绑需要路由,视图,控制器等。将它用于实体只会有点奇怪。

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). 选项2似乎没问题,因为对于/ src文件夹中的所有内容,/ app文件夹无论如何都被视为“公共”(因为,例如,参数也驻留在那里)。 However, there is no 'model' folder there, and I'm not sure that there should be? 但是,那里没有'model'文件夹,我不确定应该有吗?

I understand that there are very few 'best practices' out already for Symfony 2, since it's brand new. 据我所知,Symfony 2已经很少有“最佳实践”,因为它是全新的。 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 迪特

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. 没有需要有路由,控制器,视图等。我确实遇到过一个蓝图捆绑,并且它是所有船蓝图CSS资源,这样他们可以在项目中轻松地重用。

As for adding models to the app directory... I wouldn't like that. 至于将模型添加到app目录......我不喜欢这样。 I see the app directory as a place where all the configuration should be. 我将app目录视为所有配置应该是的地方。 Even though you can override views under app/Resources , whenever I want to override something I create a new bundle. 即使您可以覆盖app/Resources下的视图,每当我想覆盖某些内容时,我都会创建一个新的bundle。

I haven't used this technique myself in a real world symfony2 app - but it's a pointer as you asked for any feedback. 我没有在现实世界的symfony2应用程序中使用这种技术 - 但它是一个指针,因为你要求任何反馈。

The service container seems to be the Smyfony2 method to make services available globally. 服务容器似乎是Smyfony2方法,可以在全球范围内提供服务。 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. 但是,我假设模型对于所有bundle都不是完全对称的,因此我们可以将共享模型放入一个单独的bundle中,并将bundle特定的实体放入bundle本身。 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. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM