使用 PHP MVC 制作模型工厂
我基于一些扎实的教程并大量借鉴了 Cake 的经验,编写了一个不错的 MVC 框架。它适合我们的需求,因为我们已经有一个正在构建的现有应用程序。
我的情况如下:我有一个模型,描述了一个团队。这个团队有属性、方法等等。在我的控制器中,我可以通过以下方式轻松创建一支球队:
$team = new Team('Hornets')
它效果很好。但是,假设我想要一种返回给定国家/地区的所有球队的方法?在使用 MVC 之前,我只会创建一个工厂类并执行以下操作:
$teams = Array();
$teams = TeamFactory::getTeams('Canada')
这将返回一组团队。我想我仍然可以做到这一点,但我的基本问题是我将在文件夹结构中保存这些工厂类吗?
我在根目录中有一个名为“lib”的文件夹,其中保存了我经常使用的各种类。我唯一担心的是,该文件夹会变成一堆混乱的“Team.Factory.php”、“Player.Factory.php”,与所有其他内容混合在一起。我自己的直觉是也许在“lib”内创建一个名为“factory”的子文件夹,然后继续。
这是“非 MVC”吗?酰胺此仪式?
I have written a decent MVC framework based on some solid tutorials and borrowing heavily from Cake. It suits our needs because we already have an existing app onto which we are building.
My situation is as follows: I have a model that describes say, a Team. This team has properties, methods, blah blah blah. In my controller I can create one team quite easily by:
$team = new Team('Hornets')
It works great. However, let's say I want to have a method that returns all teams for a given country? Before using MVC I would just make a factory class and do:
$teams = Array();
$teams = TeamFactory::getTeams('Canada')
This would return an array of teams. I guess I can still do this, but my basic question is where would I keep these factory classes in my folder structure?
I have a folder called "lib" in the root where I keep various classes that I use on a regular basis. My only concern being that folder is going to become a big jumbled mess of "Team.Factory.php", "Player.Factory.php", mixed in with all the other stuff. My own instinct is to maybe make a sub-folder inside "lib" called "factory" and then go forth.
Is this "un-MVC"? Amidoingthisrite?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MVC 不介意目录结构。我会将 TeamFactory 类保留在与 Team 类相同的目录中,或者在其中创建一个子目录并将其放置在那里。这就是我的意思:
或者
MVC doesn't mind directory structure. I would keep the TeamFactory class in the same directory as the Team class, or maybe make a subdirectory in it and place it there. This is what I mean:
or