Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
My best guess is that View is a bunch of formatting functions, and Control uses both the Method object and the View functions to give you web pages. Am I right?
You're quite right.

If I'm not mistaken, in the original MVC design, the controller is responsible for setting up the view and connecting it to the model, and after that the View and the Model work mostly by themselves. But since web based applications behave differently from typical GUI applications, what tends to happen is subtly different:

Browser View Controller Model . . . . . HTTP Request . . . +---------------------------------->+ . | . | update model . | . +-------------->+ | . | | | . | return status | | . +<--------------+ | . select view | . | +<----------------+ . | | . . | | query state . . | +-------------------------------->+ | | . | | | . return state | | +<--------------------------------+ | HTTP Response | . . +<----------------+ . . . . . .
(see also http://zeekat.nl/joost/mvc-web/index.html)

Basically, the controller interprets the (http) requests as model actions/queries and then passes (parts of) the model* to the appropriate view (which is selected by the controller and does nothing more than display part of the model).

* By model here I mean all of the objects and classes that make up the API of whatever is being manipulated by the program - i.e. a database, sensors, a tv-card, whatever.

For example, I want a list of everyone who has blue hair. The control might be bluehair.pl, a user callable CGI. It would instantiate a bunch of User (or Method::User) objects and use User->has_blue_hair() to pick the ones that had blue hair. Then maybe it would use CGI.pm to present the View.
Yes. You've got the basic seperations right. But in most MVC frameworks in use today you would probably put most of the specifics of bluehair.pl (which might be VERY little, if you're using a good framework) in a subclass of the generic controller class and you'd use a templating system to generate the views.

Also, if you're using a relational database, it's a lot more efficient to use an SQL query (probably implemented as a Model::User class method, and likely generated by an OO-relational library) to select a list of blue haired people instead of iterating through all users in the database one by one.

updated: minor corrections.


In reply to Re^4: Implementing Model-View-Controller by Joost
in thread Implementing Model-View-Controller by ghferrari

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found