Page 1 of 1

Queries regarding MVC

Posted: Sun Sep 07, 2008 1:24 am
by leon_nerd
Hi Guys,

I am a designer & PHP programmer and have been designing & developing web applications in PHP for 7+ months. Now, I have came across the term MVC a lot of time. When I came across it I went to Wikipedia and checked out what is MVC. Model-view-controller, as it is called was not very clear to me. What I understood was this is a concept that is used to keep the designing and programming aspects independent of each other. Am I right? Along with this question I have few more questions:

1.) How does using MVC Framework differ from a general Designing & Development process?
2.) What advantages it gives us?
3.) What problems can occur if we don't use MVC?
4.) What are the most programmer friendly MVC frameworks available for PHP? Which one is best recommended for beginner?
5.) I have heard about people writing their own MVC. Why do they do so? Are there any advantages of doing so? How easy or difficult it is to write our own MVC Framework?
6.) How to develop our own MVC framework? This question is purely to understand the concept of MVC deeply.

Thanks in advance :).

Re: Queries regarding MVC

Posted: Sun Sep 07, 2008 2:11 am
by josh
leon_nerd wrote:How does using MVC Framework differ from a general Designing & Development process?
define general design. Procedural?
leon_nerd wrote:2.) What advantages it gives us?
maintainability
leon_nerd wrote:3.) What problems can occur if we don't use MVC?
this is a matter of opinion, decide for yourself if its right for you
5.) I have heard about people writing their own MVC. Why do they do so? Are there any advantages of doing so? How easy or difficult it is to write our own MVC Framework?
everyone has different reasons, licensing, maintainability, niche purposes.. if you have something that meets all your needs then dont re-invent the wheel, some people may implement their own just for the learning experience
6.) How to develop our own MVC framework? This question is purely to understand the concept of MVC deeply.
too broad of a question. a framework could be 20 lines of code or 20 million
try searching google
http://www.google.com/search?q=model+vi ... US235US235

Re: Queries regarding MVC

Posted: Sun Sep 07, 2008 3:03 am
by leon_nerd
Thanks for your answers.

By the general designing & development procedure I mean designing website in photoshop, slicing it, using a PHP editor or notepad to create PHP pages where all the files are normally in a single folder.

Re: Queries regarding MVC

Posted: Sun Sep 07, 2008 6:43 am
by josh
In that case, what it would change about your method, is the PHP code ( models & controllers ) would no longer be intertwined or dependent on the views ( and or templates ).

Check out some open source template systems. Here is a helpful resource that I think will help you open your mind to these new ideas
http://www.onlamp.com/pub/a/php/2004/07 ... tml?page=2

Re: Queries regarding MVC

Posted: Sun Sep 07, 2008 3:04 pm
by Christopher
leon_nerd wrote:What I understood was this is a concept that is used to keep the designing and programming aspects independent of each other. Am I right?
Not based on standard meanings for 'designing' and 'programming', so you really need to get some understanding about what MVC means first. However I think you may be gleaning one of the key goals of MVC and all other such architectural patterns. If by 'programming' you mean the information and business rules of the application; and by 'designing' you mean the presentation of that information -- then yes, you are on the right track.

I think one of the points of confusion when reading about MVC is that, because it was a response to traditional N-Tier architectures, MVC is often called "triangular" (to differentiate it from 3-Tier). In fact, if compare MVC and 3-Tier you find that what MVC really does is follow the 3-Tier where the Data Tier == DataSource Layer and Logic Tier == Model Layer. But MVC divides the Presentation Tier into two parts -- the Controller that deals with the Request and program flow, and the View that deals with presentation logic and the Response.

Where 3-Tier is an "I" shaped stack with Presentation Tier sitting on the Logic Tier sitting on the Data Tire -- MVC is a "T" shaped stack with the Controller and View at the top in the Presentation Layer sitting on the Model Layer sitting on the DataSource Layer. It is really important to understand that the Model is in one layer and the View/Controller are in a different, higher layer. This is essential to understanding MVC. The separation between View and Controller is less important (but more often argued ;) ). In fact if you combine the View and Controller you are really just doing 3-Tier -- which is a proven architecture.

There key to both of these architectures is the separations. They are the essence of Modular Programming. The ultimate Best Practice is to separate the Models/BusinessLogic from the Presentation code of the system. That gives you many proven benefits in stability, maintainability, extendability, portability, reuse, etc. because it isolates the informational foundations of the application from the whims of displaying it. I do want to note that MVC is actually two separations (not three). The first, and most important, separation is between the Model and Presentation layers. The second separation is inside the Presentation layer between the View and Controller.

You also need to separate MVC from MVC frameworks. The latter can support MVC and ususally do it by implementing a Front/Action Controller system that is a good fit with MVC. The Action Controller then loads Model and View object. This system encourages/enforces the two separations.

Re: Queries regarding MVC

Posted: Wed Sep 10, 2008 1:11 pm
by phice
An MVC model isn't that hard to learn. Models house all of your data manipulation functions (database queries, file editing, image manipulation, etc). Views are your template files (I like to use Smarty to make them as powerful as they should be). Controllers are their name: they control everything. Basically I setup mod_rewrite to route to my controller files from fancy urls, the controller includes the necessary model files for that section of the site, then I use a switch() logic to handle the type of _REQUEST['action'] that is set from mod_rewrite. At the bottom, I have smarty run the set $view template file.
What I understood was this is a concept that is used to keep the designing and programming aspects independent of each other. Am I right?
That's absolutely correct (in the sense of how I've designed mine). For a strong site, you like to have a programmer work with models and controllers, and your designer can update the design through view files without screwing up any of the programming, and vice versa.

Re: Queries regarding MVC

Posted: Wed Sep 10, 2008 2:46 pm
by allspiritseve
phice wrote:Models house all of your data manipulation functions (database queries, file editing, image manipulation, etc).
The model includes much more than that... essentially, any domain/business logic should go in the model. What does your site do, in business terms? Is it a blog? Financial application? Route tracker? Models should generally be the applicable nouns in those areas-- posts, authors, money, routes, etc. The database really shouldn't be included in the model, but thought of as a place to persist the model. Unfortunately, due to the popularity of ActiveRecord, many people think model=db, which isn't always true. My preference is to use a service, such as a data mapper or table data gateway, to keep persistence logic out of the domain layer.
phice wrote:Views are your template files
Views can also include php, as long as it is presentation logic. If that code goes in templates, that's fine. It could also be separated into distinct view objects that render the model for display.

Re: Queries regarding MVC

Posted: Wed Sep 10, 2008 3:30 pm
by Christopher
allspiritseve, I agree and agree. :)

Re: Queries regarding MVC

Posted: Wed Sep 10, 2008 3:31 pm
by allspiritseve
arborint wrote:allspiritseve, I agree and agree. :)
sweet :D

Re: Queries regarding MVC

Posted: Sun Sep 21, 2008 9:11 am
by thinsoldier
allspiritseve wrote:
phice wrote: The database really shouldn't be included in the model, but thought of as a place to persist the model. Unfortunately, due to the popularity of ActiveRecord, many people think model=db, which isn't always true. My preference is to use a service, such as a data mapper or table data gateway, to keep persistence logic out of the domain layer.
Do you create an instance of you data mapper object inside your controllers or inside your models?

Re: Queries regarding MVC

Posted: Sun Sep 21, 2008 9:17 am
by allspiritseve
thinsoldier wrote:
allspiritseve wrote:
phice wrote: The database really shouldn't be included in the model, but thought of as a place to persist the model. Unfortunately, due to the popularity of ActiveRecord, many people think model=db, which isn't always true. My preference is to use a service, such as a data mapper or table data gateway, to keep persistence logic out of the domain layer.
Do you create an instance of you data mapper object inside your controllers or inside your models?
Controllers. That way you could potentially use several different mappers for the same domain object.