SitePoint Sponsor

User Tag List

Results 1 to 23 of 23

Thread: MVC and views: something to get my head around

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Canberra, Australia
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MVC and views: something to get my head around

    Ok, I'll try and explain this as best I can. Say I have a web app framework in the MVC style, with front controller, actions and commands, model, and views to render. The views are PHP pages, but I'm trying to keep the views simple and as "free" of logic coding as possible, for example:

    PHP Code:
    <table>
     <?php foreach ($this->template['contacts'] as $contact): ?>
     <tr>
      <td><?php echo $contact->getName() ?></td>
      <td><?php echo $contact->getPhone() ?></td>
     </tr>
     <?php endforeach ?>
    </table>
    Simple "get this template variable and dump it out" style stuff. An executed command linked to an action would populate the 'contacts' array in this example.

    Now, say that the contacts page had an included header and footer. Let's say in the header we have a piece of template code that dumps out a "message of the day" template variable or something, i.e.:

    PHP Code:
    <p><?php echo $this->template['motd'?></p>
    The command executed to return the list of contacts would access the model layer and populate the template with the contacts... but, how does the message of the day variable get loaded into the template? It's not related to the command/action being executed... what is the best way to approach this without actually calling the model from the view, e.g:

    PHP Code:
    <p><?php echo $this->model['motd']->getCurrentMOTD() ?></p>
    Or is that what will have to be done? Does any of this make sense?

  2. #2
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The controller layer receives input, manipulates the domain (if there's any manipulating to do), and tells a view to display itself. The view grabs the data it needs and prints it. If there's some kind of business logic to do, the view gets data from domain objects (the model in MVC). If not it goes straight to the data access layer.

    The view is presentation layer so it can freelly make read-only calls to the domain layer / model but it's not allowed to manipulate the domain. The message of the day would just be made available to the template same as the contacts list and any other dynamic data you need.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Canberra, Australia
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply McGruff. So what you're saying is that rather than having to do something with the MOTD in each command/action, the view itself is responsible for pulling that data from wherever it comes from? Could a view helper be of use in this instance?

  4. #4
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes indeed.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Canberra, Australia
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, thanks. I guess my biggest problem with all the MVC and patterns stuff is not so much the concepts themselves, but just confusion over "what goes where" and what certain components/layers should and shouldn't be doing. Only thing is, each thread/article I read on it I just get more confused, lol.

  6. #6
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MVC does create a lot of confusion but really all its doing is splitting the presentation layer in the standard presentation / domain / data access layering scheme into controller and view. If you write lean classes which do just one thing that's quite likely to happen all on its own, without any forward planning.

    Also it's quiet at this time of day and no-one's around to disagree with me.

  7. #7
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    United Kingdom
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have used a Chain of Responsibility and a Composite to provide common elements to the View, rather than have the View query the Model. I'm not sure what a ViewHelper does but I'll sure have a look.

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrike
    Model. I'm not sure what a ViewHelper does but I'll sure have a look.
    Beware - ViewHelper is a loaded term. It can mean slightly different things depending on who's using it and even in which context.

    In this case, the ViewHelper is a façade to the model. This actually makes the ViewHelper (in this sense) part of the controller-layer.

  9. #9
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    In this case, the ViewHelper is a façade to the model. This actually makes the ViewHelper (in this sense) part of the controller-layer.
    Gathering data for a view seems to me to be a view responsibility. If the controller layer is involved in that I don't see the V/C split required by MVC.

    Not that you have to use the MVC pattern of course. The important thing is to separate presentation and domain.

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The ViewHelper for me (personally) is used to separate presentational logic from the View it's self, but there are other cases for this pattern as well, for example I've suggested in another thread recently that it could be employed to bind data from a Model, to an XML structure?

    PHP Code:
    <p><?php echo $this->model['motd']->getCurrentMOTD() ?></p>
    In an event such as this, as suggested I too would be using the Composite pattern, though remember that the composites are primitive, what I mean is that all composites are much all the same. In this particular case, what your actually looking for is a 'leaf', which is a composite without children

  11. #11
    Can we go to a 48 hour day?
    Join Date
    May 2002
    Location
    MI
    Posts
    906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've started battling with this. I have my object, objectDAO, a BO that handles things like default values and such and feeds them to the DAO.

    I've started trying to figure out the controller/view part. I plan on using savant templating and figure I need a view controller/helper to pick the proper template and assign the values to the templates. The controller would then just take in the REQUEST obect, clean it, and pass it into the View controller to make the decisions about what view is called for.

    My only problem is that now I have the ViewController using the BO to do the work (get the data needed for the view such as a recordset) and assign values to the template so I'm not sure if I've gone wrong with this or not. For some reason I thought the controller should be doing that work and passing results to the View.

    Am I losing it?
    mitechie.com
    "Techies just think a little differently
    ...at least that is what they keep telling me."

  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Canberra, Australia
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone recommend a good link to an example/discussion about the Composite pattern? (is this the same as Composite View?)

    Cheers,

    {R}

  13. #13
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @RainMaker:

    http://www.exciton.cs.rice.edu/JavaR.../composite.htm

    The Composite Pattern is not Composite View. Composite View however is the Composite Pattern with View Objects in an MVC environment.

  14. #14
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the following thread that I started, I posted script which constructed a Composite structure for you based on an XML file, which you could learn from?

    If your starting out to learn this pattern, it'll help to make some sense of it all

    http://www.sitepoint.com/forums/showthread.php?t=248721

    Also, here is the same technique, but from a different angle (Database, not Xml), which will balance things out for you,

    http://www.sitepoint.com/forums/showthread.php?t=261258

    Any questions, let me know and I'll try to answer them for you.

  15. #15
    SitePoint Enthusiast willthiswork's Avatar
    Join Date
    Feb 2003
    Location
    italy
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My only problem is that now I have the ViewController using the BO to do the work (get the data needed for the view such as a recordset)
    Err.....I always thought that was the way to go with MVC.
    What makes you think this approach is not correct?

  16. #16
    Can we go to a 48 hour day?
    Join Date
    May 2002
    Location
    MI
    Posts
    906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by willthiswork
    Err.....I always thought that was the way to go with MVC.
    What makes you think this approach is not correct?
    I was under the impression that the Controller would just pass in the required parts (results from the BO and such) to the View and not the entire BO. If I'm mistaken and I have it setup correctly then sweet, but I'm not sure. That's why I was asking if I was on the right track or not.
    mitechie.com
    "Techies just think a little differently
    ...at least that is what they keep telling me."

  17. #17
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know it's not proper to ask such a question here,but I really can't find a

    website address that classifies different kinds of webstation from which I can

    choose from. I 'm a foreigner ,so I'm not familiar with that.It's really my great

    favor if you could tell me about it.

    I come to this forum for the first time,it's a good place. I'm a little busy

    with my job lately,so I come here seldom this couple of days.Would you

    please send it to my mail-box ,my e-mail address is:

    randolph_shen@lycos.com


    Thanks a lot!

  18. #18
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Send what to your email address? I don't have a clue what your asking I'm afraid... If your ordering a coffee or a bacon sandwich, then there isn't much I can do to help you

    But seriously, if you can make another post, and say what your problem or question is more clearly, then maybe we could, in fact, help you

  19. #19
    SitePoint Enthusiast matid's Avatar
    Join Date
    May 2005
    Location
    Knurow, Poland
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We are talking about Views, so I'll ask my questions:
    Should View pull the Model or should the Model be pushed to View?
    Can View work on (get data from) multiple Models?
    Should Command store the Model it modified and somehow pass it to View? Or should it save it, and let View look for it?

  20. #20
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matid
    Should View pull the Model or should the Model be pushed to View?
    That probably hasn't got a straight answer. If you are being really strict about separation of concerns, pushing is the only allowed way. This is because the moment you let the view pull data, you are letting the view do the controllers job. (or some of the controller job)
    In reality, it may be a good idea to allow this relaxation of the separation, especially when the controller's role is very simple, and really just reduced to transfer data from model and push it to the view.

    Quote Originally Posted by matid
    Can View work on (get data from) multiple Models?
    It's a common confusion to identify model with a single class. In the MVC pattern, model is an abstract layer of your application.
    It may be made up of several objects, as in an object-model, or by a few tablegateways. Either way, the view can certainly use data from several objects (or gateways)

  21. #21
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matid
    Should View pull the Model or should the Model be pushed to View?

    Can View work on (get data from) multiple Models?

    Should Command store the Model it modified and somehow pass it to View? Or should it save it, and let View look for it?
    1) A matter of preference, really. If you do it right the two could be represented with the exact same code in the View.

    2) It can, but I'd recommend separating the areas that access the model to well-defined parts so that eg. if you have a ViewComposite tree, each View uses only one Model. The resulting "View" would then seem to have accessed different Models at once even if they're neatly separated in the code.

    3) There are two things here. The first is that the Controller layer should access the Model for any modifications on existing data, and then generate the View. The actual question, the latter part that is, was answered in point one.

    Quote Originally Posted by kyberfabrikken
    It's a common confusion to identify model with a single class. In the MVC pattern, model is an abstract layer of your application.
    Oh. I've tended to think of that as the model domain, and model a single accessor to that layer. It could be a gateway or whatever.
    Last edited by Ezku; Aug 19, 2005 at 08:18.

  22. #22
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    It's a common confusion to identify model with a single class. In the MVC pattern, model is an abstract layer of your application.
    Yep. As MVC is a User Interface pattern, I tend to treat the Model as everything you would be left with if you didn't have a user interface.


    Douglas
    Hello World

  23. #23
    SitePoint Enthusiast matid's Avatar
    Join Date
    May 2005
    Location
    Knurow, Poland
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    It's a common confusion to identify model with a single class. In the MVC pattern, model is an abstract layer of your application.
    It may be made up of several objects, as in an object-model, or by a few tablegateways. Either way, the view can certainly use data from several objects (or gateways)
    Sure thing, I just wanted to know whether single View should be able to access multiple model objects.

    Now lets take it to the code:
    PHP Code:
    <?php

        
    class IndexCommand extends BaseCommand
        
    {
            public function 
    execute()
            {
                
    $Message = new Message( new String'Blank Index Page' ), new String'Lorem ipsum dolor sit amet...' ) );
                
                
    $this->addModel( new String'Identifier' ), $Message ); // is this any good?
                
                
    $this->setViewHandle( new ViewHandle( new String'IndexSuccessView' ) ) );    
            }
        }

    ?>
    Then I can access it in View like this:
    PHP Code:
    <?php
    $Message 
    $this->getModel( new String'Identifier' ) );
    ?>
    What do you think about it? Is this any good? Or should I change it? How would you do it?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •