MVC 框架,模型应该类似于“模块”结构吗?
我正处于使用 PHP MVC 框架 CodeIgniter 构建网站的早期阶段。到目前为止,我的模型相当简单,但我已经怀疑我的编码方法。让模型互相使用是一个坏主意吗?虽然它确实减少了所需的代码量,但它也增加了每个模型方法中对外部代码的强烈依赖。这样可以吗?或者这些方法是否需要一种更加“模块”的方法来让它们独立于其他方法而工作?
感谢您抽出时间。
I'm in the early stages of building a website using CodeIgniter, a PHP MVC framework. My models are, so far, fairly simple, yet I'm already doubting my coding approach. Is it a bad idea to let models use eachother? While it certainly reduces the amount of code needed, it also raises a strong dependency on outside code within each model method. Is this okay? Or does the methods need a more 'module' approach where they just work, independent of other methods?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理论上,模型应该组合成独立的模块。然后,您应该能够将该模块从 CodeIgniter 应用程序移动到 CodeIgniter 应用程序,并且它应该无需任何修改即可工作。
所以模型之间互相使用是可以的。模型应该代表现实世界的对象,而现实世界的对象也相互依赖!
In theory, models should be grouped together into independent modules. You should then be able to move that module from CodeIgniter app to CodeIgniter app and it should work with little to no modification.
So it's OK for models to use each other. A model should represent a real-world object, and real-world objects rely on each other too!
是的,有两种方法可以做到这一点,
一种是模型调用(实例化)新对象本身
,或者您可以让控制器调用(实例化)对象并将其传递给您的模型,
以太可以工作,但我个人更喜欢这样做第一个
Yea There is 2 ways to do it how ever,
There is the model call (Instantiate) the new object its self
Or you can have your controller call (Instantiate) the object and pass it to your model
ether works but my personal preference to do the first