Why Do We Use MVC Frameworks?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
supermike
Forum Contributor
Posts: 193
Joined: Tue Feb 28, 2006 8:30 pm
Location: Somewhere in the Desert, USA

Why Do We Use MVC Frameworks?

Post by supermike »

Why do we use MVC-type PHP frameworks like Zend Frameworks and other frameworks?

I mean, MVC gives you model, view, and controller. The view is where you put your XHTML for display on the screen. The model is where you put the meat of your logic. The controller is meant to be the glue between meat and display, or, rather, model and view. Sounds great, huh? Might eliminate some spaghetti code, huh?

Okay, the main point is that you want to separate your PHP from your XHTML from your SQL. Of course, if you did that completely, you'd never have a functioning program, so of course you have to mix this slightly. Several people got along just fine by putting their XHTML into Smarty or in a separate PHP file that they treated like a template and included it and the end of their programming logic with minimal PHP logic going on inside the template. Next, several people got along just fine by parameterizing their SQL with ^ or ? (or using stored procedures if your DB supports it), sticking it in a central class file as a bunch of public properties, and then having your main PHP logic (your model, let's say) load that SQL, insert data in the parameter points, and run the SQL. And even if you do this, you can still achieve ORM type classes with property get and set on tables and then .save to save the record without having to resort to full-blown MVC.

Next, it's soooo easy in the controller to abuse the MVC framework and start putting too much business logic in there, not using the model section enough. Been there; caught myself doing that.

To me, MVC style frameworks add unnecessary bulk. Want to get started doing something better, faster, and easier, and which has faster-running code? Fine, the psudo-MVC steps are:

1. Create a templates folder and put all your XHTML templates in there, renamed as .php files. That's your 'View'.
2. Create a settings class, self-instantiating at the bottom of it to give you a $settings variable, which has public properties that expose parameterized SQL and other settings strings. That's sort of part of your 'Model'.
3. Create a set of toolbox classes categorized by topic, such as $web, $db, $strings, $dates, $xml, and $misc. In there, make it just have public methods that do particular tasks for you without the need to do subclassing and the complexity associated with that. That's sort of part of your 'Model'.
4. Now, create your PHP pages that are sort of like your 'Controller' and a little bit of your 'Model'. These load your toolbox classes, grab the SQL from the $settings class, insert variables into the parameter points, interact with the database, set some variables, and then load your template 'View' pages to insert those variables for display. And your best bet is to make the pages generated in step # 4 here as consistently looking as possible so that one can hop into any of these pages and skip to the appropriate section.

Gosh, it's simple, faster, and more efficient. Less hoops, less mess if you do this right.

So, if someone really wants to build a great framework for the PHP community, they could just stick with making more toolbox classes for us that do particular tasks, or give us a lightweight framework that uses Events (some of us may remember the old days of Visual Basic) so that we can intercept anything coming from form posts (and gets), anything coming from or going into the database, and anything being displayed on the screen, and which then automatically subclasses our own code -- providing an avenue for everyone to build modules for anyone's code. (BTW, on the latter, I hear that's what the new MoonPHP framework is all about, as are some others out there.)

And on that note, I'm wondering if the Zend Framework can be hooked such that you don't use its MVC model -- you just load its classes and use them like Toolbox classes in the psuedo-MVC model I've described above. Something to think about.

So there you have it. We mere mortal PHP programmers can simulate something MVC-like and get work knocked out faster. Trouble is -- we all do it our own way and a new framework needs to come forward that does this new vaguely-like MVC-style in a consistent way.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Why Do We Use MVC Frameworks?

Post by Christopher »

There are a number of different issues that you are mixing together here -- MVC, controller architectures, Front Controllers specifically, keeping business logic in the Model (fat-Model), etc., etc., etc. So I think your criticism might be better expressed as a criticism of the design of the frameworks you have looked at / used -- which all may implement some degree/style/take on the MVC pattern among many other things. I don't think it is much about MVC. In fact, my guess is that you really might want to keep the MVC part of some frameworks and toss out some of their architectural decisions. ;)
(#10850)
supermike
Forum Contributor
Posts: 193
Joined: Tue Feb 28, 2006 8:30 pm
Location: Somewhere in the Desert, USA

Re: Why Do We Use MVC Frameworks?

Post by supermike »

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

Re: Why Do We Use MVC Frameworks?

Post by Christopher »

Yeah. I think the two separations defined by the MVC pattern are really important to follow. But there seems to be a big difference between the meaning of 'MVC pattern' and 'MVC framework'. ;)
(#10850)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Why Do We Use MVC Frameworks?

Post by superdezign »

Endless rant aside, "we" (regardless of which "we" you are referring to) use frameworks to avoid the necessity of rewriting code. You can write your own framework, and it doesn't have to be a MVC pattern either. Frameworks are meant to simplify (or simply organize) the writing of other code which takes advantage of the framework's features. The benefits of MVC is the separation, as mentioned by ~arborint. All you really need is the Model and the View, but if you want user interaction, the Controller is pretty necessary.

Your complaint seems targeted at robust frameworks. You can make a simply MVC framework that have a central index.php file as the front controller, which includes other *.php files as action controllers, which include business-logic class files, and then at the end of the index.php file, output the template using whatever global variables you created from the controllers and models. It doesn't HAVE to be complex... it just has to work.

Of course, it seems to me like you just felt like ranting. :P
Post Reply