Author Topic: Help me understand why MVC is beneficial  (Read 4227 times)

0 Members and 1 Guest are viewing this topic.

Offline sKunKbadTopic starter

  • Devotee
  • Posts: 1,477
  • Gender: Male
    • View Profile
    • Brian's Web Design - Temecula
Help me understand why MVC is beneficial
« on: April 15, 2009, 02:38:07 PM »
I'm exploring the Kohana PHP framework, and I can see how views, controllers, and the template work together, but I haven't had time to figure out the use of models.

I'm used to procedural PHP, and while I understand and use OOP, MVC of Kohana takes me to a new level of feeling stupid. I really don't understand the need for MVC at all.
Brian's Web Design - Temecula

Freedom is only available through death.

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Help me understand why MVC is beneficial
« Reply #1 on: April 15, 2009, 03:24:11 PM »
Theoretically you could take any part of MVC and put something else in its place without changing other parts.

It's easiest to imagine when we consider View. You could have one View that generates fancy, colourful, CSS styled HTML Web pages for traditional browsers, and another that generates plain Web pages for mobile devices with low bandwidth. Both would be using same Models and same Controllers. You don't have to introduce any changes into Models or Controllers if you want to switch the view. You can also easily add another view for generating RSS, then another for.. .something else...

On the Model side one could easily imagine the need to switch from MySQL to Oracle to PostgreSQL... With MVC pattern switching a database engine should not require any changes to Controllers or Views.


NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline sKunKbadTopic starter

  • Devotee
  • Posts: 1,477
  • Gender: Male
    • View Profile
    • Brian's Web Design - Temecula
Re: Help me understand why MVC is beneficial
« Reply #2 on: April 15, 2009, 06:41:49 PM »
Those are helpful facts about MVC. Thanks for your reply.

Would you personally use MVC in all projects, regardless of size?
Brian's Web Design - Temecula

Freedom is only available through death.

Offline keeB

  • Nick Stinemates
  • Staff Alumni
  • Devotee
  • *
  • Posts: 1,130
  • Gender: Male
  • I'm no good at this
    • View Profile
    • A little dash of life..
Re: Help me understand why MVC is beneficial
« Reply #3 on: April 16, 2009, 12:14:35 AM »
Yes. Small projects grow to large projects without rhyme or reason.

Writing tag soup PHP pages makes maintenance a nightmare, while writing a proper model has little overhead. There's *zero* reason to not implement a design pattern like MVC.

Unless of course you're not writing a website.
Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/

Offline ambertch

  • Irregular
  • Posts: 4
    • View Profile
Re: Help me understand why MVC is beneficial
« Reply #4 on: May 14, 2009, 08:57:52 PM »
Yes. Small projects grow to large projects without rhyme or reason.

Writing tag soup PHP pages makes maintenance a nightmare, while writing a proper model has little overhead. There's *zero* reason to not implement a design pattern like MVC.

Unless of course you're not writing a website.



Do you happen to have a link somewhere with examples of functionalities/things which should be part of the m, v, c?

Like for example displaying a sortable table. I could definitely say the CSS and non-table html is part of the view... the controller has the db abstraction layer? And what about the php that generates the html for the table? The table's a view, but content should be generated by the model.

Yeah, I'm confused  >:(

Offline GarrettW

  • Irregular
  • Posts: 14
  • Gender: Male
    • View Profile
    • GarrettW.net
Re: Help me understand why MVC is beneficial
« Reply #5 on: May 15, 2009, 07:24:32 AM »
ambertch, here's my understanding of it, as it pertains to your sortable-table scenario.

The Controller is where the "business logic" takes place ... i.e. all the computation and processing. The core of the application, you could say.
The DB abstraction layer is NOT in the Controller - it is in the Model. the Model is the link between a Controller and the raw data.
You are correct that the HTML & CSS are part of the View, but the PHP that generates the HTML is also part of the View. The View takes the Controller's output and formats it for display.

someone please correct me if i'm wrong, as I'm just learning this stuff too.

Offline nadeemshafi9

  • Devotee
  • Posts: 1,271
  • Gender: Male
    • View Profile
Re: Help me understand why MVC is beneficial
« Reply #6 on: May 15, 2009, 07:51:46 AM »
Theoretically you could take any part of MVC and put something else in its place without changing other parts.

It's easiest to imagine when we consider View. You could have one View that generates fancy, colourful, CSS styled HTML Web pages for traditional browsers, and another that generates plain Web pages for mobile devices with low bandwidth. Both would be using same Models and same Controllers. You don't have to introduce any changes into Models or Controllers if you want to switch the view. You can also easily add another view for generating RSS, then another for.. .something else...

On the Model side one could easily imagine the need to switch from MySQL to Oracle to PostgreSQL... With MVC pattern switching a database engine should not require any changes to Controllers or Views.

i like the browser small device example

it woudl pan out like this

http://domain/acontroler-eg-houses/a-method-that-outs-a-view-using-a-controler-method-inside-wich-there-is-model-interactivity-FOR-BROWSER/somvarname/varval
http://domain/acontroler-eg-houses/a-method-that-outs-a-view-using-a-controler-method-inside-wich-there-is-model-interactivity-FOR-MOB/somvarname/varval

http://domain/acontroler-eg-person/a-method-that-outs-a-view-using-a-controler-method-inside-wich-there-is-model-interactivity-FOR-BROWSER/somvarname/varval
http://domain/acontroler-eg-person/a-method-that-outs-a-view-using-a-controler-method-inside-wich-there-is-model-interactivity-FOR-MOB/somvarname/varval

http://domain/acontroler-eg-person/another-method-that-outs-a-view-using-a-controler-method-inside-wich-there-is-model-interactivity-FOR-BROWSER/somvarname/varval
http://domain/acontroler-eg-person/another-method-that-outs-a-view-using-a-controler-method-inside-wich-there-is-model-interactivity-FOR-MOB/somvarname/varval

when you use these its apparent how much better it is, you can move functiuonality about easily everything is encapsulated and ajax becomes much more simple using this format of url pluss its google friendly

extjs

Quote
Ext.Ajax.request({
   url: '/person/getname/id/1',
   success: someFn,
   failure: otherFn,
   headers: {
       'my-header': 'foo'
   },
   params: { foo: 'bar' }
});


Ext.Ajax.request({
   url: '/person/getaddress/id/1',
   success: someFn,
   failure: otherFn,
   headers: {
       'my-header': 'foo'
   },
   params: { foo: 'bar' }
});


i use the zend_framework it comes fully strapped with functionaly directory structure classes and a bootstrap file mod rewrite app.ini to controle configuration complete rewrite of all php constructs

http://framework.zend.com/manual/en/
« Last Edit: May 15, 2009, 07:56:58 AM by nadeemshafi9 »
DONT LAY AN EGG OVER IT

Offline spaze

  • Irregular
  • Posts: 29
    • View Profile
Re: Help me understand why MVC is beneficial
« Reply #7 on: June 19, 2009, 05:19:20 AM »
I created my own blog with ZF (http://www.majgaj.com/blog) and when I finished my project I realized I was missing the RSS feeds function. Since I had View and Models set, all I had to do was create rssAction() function into my controller and create new View for the actual RSS feed. All in all this RSS addition took me five minutes.

If I would like to make a lighter layout for, say, mobile device, it would also take me five minutes to accomplish this.

This is why I really like MVC design pattern.

Offline cornofstarch

  • Irregular
  • Posts: 32
  • Gender: Male
    • View Profile
Re: Help me understand why MVC is beneficial
« Reply #8 on: June 28, 2009, 09:22:45 PM »
I'm exploring the Kohana PHP framework, and I can see how views, controllers, and the template work together, but I haven't had time to figure out the use of models.

I'm used to procedural PHP, and while I understand and use OOP, MVC of Kohana takes me to a new level of feeling stupid. I really don't understand the need for MVC at all.

Slightly off-topic...

My 2 cents: you can also use MVCL (L for language) which can possibly, in the future, assist you in translating sites into different languages without hassle.