Minimalistic MVC Framework

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Minimalistic MVC Framework

Post by Christopher »

VirtuosiMedia wrote:Since the topic is a minimalistic MVC framework, what do you think one would have to include, bare minimum? And a related, but separate question: what would one have to include bare minimum for you to use it?
The minimum is to just have the three objects in the same PHP file and have the dependencies right. That would be a Page Controller.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Minimalistic MVC Framework

Post by josh »

@scotty,

you have separated out the presentation logic from the business logic. the most important separation. Next is to separate the control flow from the business logic ( or more precisely, the business logic from the control flow ). Anything that is a noun in the conceptual representation of your system should be a model object, so that it can use a consistent API for access from different controller scripts. You could also "pull" your models from the template, bypassing the controller, but that's getting more advanced
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Minimalistic MVC Framework

Post by Christopher »

jshpro2 wrote:you have separated out the presentation logic from the business logic. the most important separation.
Yes, if you do nothing else, do this.
jshpro2 wrote:Next is to separate the control flow from the business logic ( or more precisely, the business logic from the control flow ).
I think you meant separate the control flow from generating the response back to the browser. Business logic should stay in the Model.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Minimalistic MVC Framework

Post by josh »

arborint wrote:I think you meant separate the control flow from generating the response back to the browser. Business logic should stay in the Model.
:oops: yeah you're right, to some degree the model can contain logic about flow, in the form of dependencies in the model layer ( models using other models or interfaces ).

Controller = processing inputs, or program events, control should be passed to a model ( a "thin" controller )
controller pushes model to view, view optionally grabs or "pulls" additional models from the model layer via view helpers or other framework specific construct
view generates output, allowing the user to trigger new events ( which get intercepted by a controller.. cycle repeats until user is happy )

the advantages of separating the controller are not only abstraction such as having controllers inherit from some base controller that provides common functionality, but the ability to break your dispatch cycle down into events that the front controller fires ( framework component that passes request to your controllers ), so you can implement controller plugins that hook into the controller dispatch cycle. Since the control is inverted ( controllers don't know about which plugins are loaded ) you remove a lot of tedious setup code higher up in the application where it belongs, out of your [page] controllers. Common functionality such as ACL which may need to augment the front controller behavior or dispatch additional controller actions is centralized following DRY principle ( dont repeat yourself ). Instead of each controller calling into the model code and making decisions, you can abstract that behavior much more cleanly with framework controllers

Using controller actions also allows you to chain together actions using forwards ( from a plugin ) where before you would have to rely on including 1 page controller from another, or using a redirect, copy and pasting code, or some other form of "hack" that introduces type coupling into the system ( either a direct literal reference from one component to another, or a dynamic reference )
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Minimalistic MVC Framework

Post by volomike »

Just to clear this up. I'm MVC all the way now. 'Took me awhile to realize I was already moving in that direction. I mean, I was already doing the View. And then when I did routing with handsome URLs, that was my front controller logic right there. When I started building CRUD classes for entities like Users, Accounts, Subscriptions, Jobs, etc. -- I realized, hey, I'm building Models. So I then said, "Why not just go all the way?"

Besides, on a really large project, I was starting to lose my mind because files were strewn all over the place. I literally had like 40 files in a given directory as the project grew, and that was beginning to get really nutty. So, MVC can help break that up a bit, especially if you have an MVC framework that lets you implement several layers of subfolders.

I started evaluating several frameworks, and each are interesting and inspiring in their own way, but I think I prefer making my own, so I did.
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Minimalistic MVC Framework

Post by volomike »

PCSpectra wrote: You know what clean means to me?

1. Small, simple well named functions. Are any of your functions over 50 SLOC (not physical)? Not clean anymore.
2. Does your code ever use more than 2 levels of nesting? Whether it's an nested IF or WHILE? More than two equals code smell.
3. Are your variables ever acronymized? $arr $cnt $res? I consider that a code smell.
4. Do your comments consume more than 5% of your physical lines of code? Yes? That I consider a code smell.
5. Do any of your functions, classes, modules, files or folders has similar names? If you have more than one directory named 'core' that is a code smell.
6. Do you have *any* HTML anywhere outside of your templates? Even a simple boldify. I consider that a code smell.
7. Do you have anywhere, more than a dozen files in any one directory? I consider that a code smell.
8. Do you have any code duplication more than 4-5 lines? I consider that a code smell.

I could go on all day but I won't cause you get the point.
No, no. Don't stop there. I found the list interesting and an influence. Please elaborate and pour it all out if you have any more views on what "clean" is.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Minimalistic MVC Framework

Post by josh »

volomike wrote:Besides, on a really large project, I was starting to lose my mind because files were strewn all over the place. I literally had like 40 files in a given directory as the project grew, and that was beginning to get really nutty. So, MVC can help break that up a bit, especially if you have an MVC framework that lets you implement several layers of subfolders.

I started evaluating several frameworks, and each are interesting and inspiring in their own way, but I think I prefer making my own, so I did.
I too, had to take 2 steps backwards before stepping forwards. I started trying to make my own MVC framework too but later realized its much easier / better to modify something close to what I like, then try to start it from scratch. If you want to put the "view" part on steroids, look into composite views, aka Magento's "block" system, its quite inspiring but poorly implemented
Post Reply