我在哪里将Symfony中的类既不是控制器也不是模型?

时间:2022-02-22 20:07:25

For my application bundle, I will need some classes that are neither controllers nor models. For instance, I would like to have a scorecard class that has members such as "skill", "efficiency", "beauty", etc. Also, it may have member method/getters like "meanScore".

对于我的应用程序包,我需要一些既不是控制器也不是模型的类。例如,我想有一个记分卡类,其中包含“技能”,“效率”,“美容”等成员。此外,它可能有成员方法/ getters,如“meanScore”。

Where would such a class go in the Symfony framework?

在Symfony框架中,这样的课程会在哪里?

3 个解决方案

#1


8  

I agree with @Gordon that this sounds like a business object. But if you're sure that it isn't, your next step is to figure out how you would classify it. Is it a helper? An event listener? A utility class? Once you figure that out, ask yourself: is it specific to a bundle, or are you going to reuse it amongst projects?

我同意@Gordon这听起来像是一个商业对象。但如果您确定不是,那么下一步就是弄清楚如何对其进行分类。它是帮助者吗?一个事件监听器?实用类?一旦你想出来,问问自己:它是一个特定的捆绑,还是你要在项目中重用它?

Let's say you decide it's an event listener and belongs to the bundle. Put it in MyBundle/EventListener. If it's a helper, put it in MyBundle/Helper. Now if you plan on reusing it amonst projects (which in this case it actually doesn't sound like it, but bear with me...) you might be better off creating a place in vendor for it.

假设您认为它是一个事件监听器并且属于该捆绑包。把它放在MyBundle / EventListener中。如果它是帮助者,请将其放在MyBundle / Helper中。现在,如果你计划重复使用amonst项目(在这种情况下它实际上听起来不像它,但请耐心等待......)你最好在供应商中为它创建一个地方。

The important thing to remember is that because Symfony2 is so young, there isn't really a definitive list of best practices that answer questions like these. Right now it's kind of up to us to see what works, and what doesn't. It's like the wild west :)

需要记住的重要一点是,因为Symfony2如此年轻,所以没有一个确定的最佳实践列表可以回答这些问题。现在,我们有责任看看哪些有效,哪些无效。这就像狂野的西部:)

#2


3  

The way I answer these sorts of questions for myself is I go to KnpBundles and check how other developers are doing it.

我自己回答这些问题的方法是去KnpBundles并检查其他开发人员是如何做到这一点的。

Another thing to note is that Symfony2 is all about freedom of choice when it comes to configuration. You can stick everything in Random folder for all Symfony2 kernel cares, as long as you set it up correctly. Well, that's probably taking it a bit to the extreme, but for example Listener or EventListener - no difference.

另外需要注意的是,Symfony2在配置方面完全取决于选择的*。您可以将所有Symfony2内核关注的所有内容都粘贴在Random文件夹中,只要您正确设置它即可。好吧,这可能会让它有点极端,但是例如Listener或EventListener - 没有区别。

Now, if you'd ask me, I'd say that for the Scorecard - depending on where you're going to use it, either Service or Helper/Util

现在,如果您问我,我会说记分卡 - 取决于您将要使用它的位置,服务或帮助/实用程序

About the breadcrumb - seems like a good example for a twig extension.

关于面包屑 - 似乎是树枝延伸的一个很好的例子。

#3


2  

Since this class apparently represents a business object from your Domain, it belongs to the Model.

由于此类显然代表域中的业务对象,因此它属于Model。

#1


8  

I agree with @Gordon that this sounds like a business object. But if you're sure that it isn't, your next step is to figure out how you would classify it. Is it a helper? An event listener? A utility class? Once you figure that out, ask yourself: is it specific to a bundle, or are you going to reuse it amongst projects?

我同意@Gordon这听起来像是一个商业对象。但如果您确定不是,那么下一步就是弄清楚如何对其进行分类。它是帮助者吗?一个事件监听器?实用类?一旦你想出来,问问自己:它是一个特定的捆绑,还是你要在项目中重用它?

Let's say you decide it's an event listener and belongs to the bundle. Put it in MyBundle/EventListener. If it's a helper, put it in MyBundle/Helper. Now if you plan on reusing it amonst projects (which in this case it actually doesn't sound like it, but bear with me...) you might be better off creating a place in vendor for it.

假设您认为它是一个事件监听器并且属于该捆绑包。把它放在MyBundle / EventListener中。如果它是帮助者,请将其放在MyBundle / Helper中。现在,如果你计划重复使用amonst项目(在这种情况下它实际上听起来不像它,但请耐心等待......)你最好在供应商中为它创建一个地方。

The important thing to remember is that because Symfony2 is so young, there isn't really a definitive list of best practices that answer questions like these. Right now it's kind of up to us to see what works, and what doesn't. It's like the wild west :)

需要记住的重要一点是,因为Symfony2如此年轻,所以没有一个确定的最佳实践列表可以回答这些问题。现在,我们有责任看看哪些有效,哪些无效。这就像狂野的西部:)

#2


3  

The way I answer these sorts of questions for myself is I go to KnpBundles and check how other developers are doing it.

我自己回答这些问题的方法是去KnpBundles并检查其他开发人员是如何做到这一点的。

Another thing to note is that Symfony2 is all about freedom of choice when it comes to configuration. You can stick everything in Random folder for all Symfony2 kernel cares, as long as you set it up correctly. Well, that's probably taking it a bit to the extreme, but for example Listener or EventListener - no difference.

另外需要注意的是,Symfony2在配置方面完全取决于选择的*。您可以将所有Symfony2内核关注的所有内容都粘贴在Random文件夹中,只要您正确设置它即可。好吧,这可能会让它有点极端,但是例如Listener或EventListener - 没有区别。

Now, if you'd ask me, I'd say that for the Scorecard - depending on where you're going to use it, either Service or Helper/Util

现在,如果您问我,我会说记分卡 - 取决于您将要使用它的位置,服务或帮助/实用程序

About the breadcrumb - seems like a good example for a twig extension.

关于面包屑 - 似乎是树枝延伸的一个很好的例子。

#3


2  

Since this class apparently represents a business object from your Domain, it belongs to the Model.

由于此类显然代表域中的业务对象,因此它属于Model。