I thought maybe I should post this in the general PHP forum although it’s not specific to PHP either, but since I was looking for the Yii way to handle things, I posted here.
Mapping all the site pages is fantastic advice. I went to great lengths to ensure all my databases/tables were normalized before I wrote a single line of code, but failed to map out the pages themselves. So my planning was only partially done. Doh. I do have lists of functionality each page will have, written many times over as things change, of course, but it’s not the same as page planning.
I think some of my confusion comes with all the ways I’m supposed to be doing things:
Keep It Simple, Stupid,
Don’t Repeat Yourself
Skinny Controllers, Fat Models
Agile, iterative development
MVC and keeping logic out of presentation
Over-thought is Over-wrought
An example:
Assume I have three web pages; one is a login page, one is a registration form, and one is a list of registrants.
Obviously I want to sanitize any input on those first two pages, but they may be in different controllers and, more importantly, use different models. To KISS, it seems like I should have a sanitize function for each model that is specific to it’s potential input, keeping it simple and self-contained. To keep things DRY however would mean a single monolithic function that serves all sanitizing events and is available app-wide. Doing the latter would make the code hard to follow since you would need to jump between multiple files to really understand what’s going on, but there would be a minimum of repeated code.
Another example, this time with the last page (registrants):
In the controller, let’s say I query a DB for a list of existing registrants (using a fat model!). In one case, a data-filled object is returned which is then passed to a view. The view loops over it and displays the list.
In the other case, no registrants are found and an empty object is returned.
If I test for emptiness in the controller, it means I need two conditional render statements, one for the data-filled object and one that ignores the empty object.
If I test for emptiness in the view, then I have logic in the presentation where it’s not supposed to be and have violated MVC.
Which do I choose?
Even with my limited experience in writing medium or larger apps, I tend to think I should keep the views as unintelligent as possible and in this way, consolidate logic in the controllers and models. But if I do that, the code in those controllers and models becomes quite ugly; lots of conditionals to find out which render statement to execute, for this example. Well, maybe not lots, but even two seems redundant and wrong (and bloated).
It’s these kinds of questions that drive me nuts. Am I looking for best practices or is this stuff related more to design patterns? I don’t know what to look for to see ‘how it’s done’ in professional apps. I realize I’m not professional caliber yet, but that’s my goal and want to get on the right track.
Thanks for helping me understand, I really appreciate any advice or pointers to information 