Jump to content

Archived

This topic is now archived and is closed to further replies.

sKunKbad

Help me understand why MVC is beneficial

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

 

 

 

Share this post


Link to post
Share on other sites

Those are helpful facts about MVC. Thanks for your reply.

 

Would you personally use MVC in all projects, regardless of size?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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  >:(

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

 

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/

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.