douhuanchi6586 2013-01-07 10:38
浏览 46
已采纳

CodeIgniter / Fat Models / Skinny Controllers

Today, I have learned that it is considered good practice to have fat models and skinny controllers. Up to now, I have had it the other way around, so everything that I believed I understood about MVC has now been proven to be wrong.

Most articles indicate fat models/skinny controllers approach is better, and the main reason I see is that they claim that controllers are not re-usable. This is true for standard installations of CodeIgniter, but when using a plugin like Wiredesignz' HMVC plugin, this becomes a non-issue. So, in short, the question is:

What is the best approach to use for Code Igniter specifically?

  1. Fat models/Skinny controllers
  2. Fat controllers/Skinny models with HMVC plugin
  3. Fat controllers/Skinny models WITHOUT HMVC plugin

I am not considering fat controllers/skinny models WITHOUT HMVC plugin as an option any more, but included it for completeness.

Your thoughts, please? Is an HMVC plugin thus an evil thing?

  • 写回答

2条回答 默认 最新

  • douxiajia6720 2013-01-07 13:52
    关注

    You are kind of right in that splitting into modules does negate some of the reasons for working SC/FM. But it is still good practice for several reasons:

    1) Re-usability of code As you said models are supposed to be reusable, the more code you can make reusable the better. Yes I know modules do this too, but the point of modules is more about code being isolated and portable, you still may want to extend those controllers at a later stage and working in a SC/FM way will help when that time comes.

    2) Code readability When reading your code most people will start from your routes config and then go to the appropriate controller. A skinny controller acts as a director and should be concise and readable. Just making calls to more complex and verbose code. Less code is easier to follow.

    3) When in Rome Not to be overlooked, the fact that everyone* who uses MVC frameworks works this way means that you should work this way too, yes its nice to be different, but if you have 5 people in the same team working together who all want to be different it's just a pain in the arse.

    Should you rewrite your code?

    Probably not immediately, I'm sure you have better things to do. If you start trying to think in this mindset from this point forward, at some point you will look back at you old code and change it (probably when you're in there changing something anyway), but for now if it aint broke...

    Further more, working in a modular way is not as straightforward as it appears, making truely reusable modular code is not easy and actually requires quite a lot of overhead compared to doing quick and dirty code for a single site. May be better to start practicing this, on fresh code until you have a good understanding of what's required to convert your existing code to something truely modular - this would be the time to sort out your fat controllers.

    When writing new code for a particular site, I always bear in mind that I may want to reuse it on another site later, but if I tried to make everything re-usable with every option under the sun I would never get anything done. Far better to go and tweak it when the time comes.

    Opinion on HMVC I'll be honest, I dont really know what the HMVC bit is about, but modules are the best thing since sliced bread, since I stumbled across HMVC wiredesignz I now do much less work for a lot more website - and much more importantly I spend a lot less time doing the same thing... What you want a gallery on your site. Heres the gallery manager module, Cms you say, yes there is a cms module, products, yes theres a products module.... you get the idea. I now have lots of variations on one framework rather than hundreds of different sites.

    Summary

    You may be dealing with simple modules at the moment which means that code re usability and clarity is not a major issues within those modules. But start coding with good practice today. And when you are working on some mammoth CMS or products module you will be glad you did.

    Check this out for a bit of a guide on what should go where How should a model be structured in MVC?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看