SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How many layers?

    I'm working on a basic website. It has a user login, member area etc... and the like. I'm wondering what I'll be comprimising if I mix the page controller logic (getting the model etc..) with the view logic:

    $tpl->set('user_name', $model->get('user_name'));

    My models are in separate files/classes of course. But I feel like I could code this thing really fast if it were all on the same controller page. This is mixing application logic (business?) with view logic though, what's going to jump out at me later? Anything?

    Matt

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IMO, setting the template vars with the output of model object is the job of the page controller. The "view logic" is the rendering of the data after the template variables have been loaded.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I see. So why are there frameworks with a controller (or action) and a view class, and then templates (html files)? Are you saying that it's OK to have the controller get the model, and set the templates, all in one scope? Basically ditching the view class?

    Matt

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have application structure with a "View" class that has common functionality to render a template using Smarty. I then subclass the view class for each of the different views. I have come to think of the View classes as actually still part of the controller, and Smarty (or whatever template engine you choose) as the actual view implementation. Given that perspective, I should probably rename the base class ViewController and then you might create instances of IndexViewController or AdminViewController, etc. But, I am lazy with my typing and am happy making the distinction mentally and not in the class name
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That makes sense. So you think that doing this in the controller is OK?

    $model =& ModelFactory::make('user');

    if( $model->get($id); )
    $type = 'success';
    else
    $type = 'fail';

    $view =& ViewFactory::make($type);
    $view->setModel($user);
    echo $view->getOutput();

    And for what I was originally was talking about... it'd be OK to completely skip the view class and output to the template engine?

    Using the view class encapsulates the view logic and template engine. I like that idea. So, when I'm refactoring my application, I could go through the code and begin to make view controller classes that have common functionality. Is that is a good approach?

    Matt

  6. #6
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks reasonable to me.

    I was never able to have a "setModel" method work for me conceptually. I typically have two or three differnent Domain Model classes that are required for populating the view before rendering. If you have a 1 to 1 relationship, then that would probably work fine for you.

  7. #7
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to think of model, view and controller as abstract concepts. In one application there is never such a thing as 'one' model, one 'controller' or one view.

    Views can accept multiple 'models' and because model is such a broad and abstract concept, a View can accept a lot of things as.. well, model

  8. #8
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you explain more...

    PHP Code:
    ...
    $view->setModel($user);
    ... 
    Exactly what does this method do ?

    Is it an Accessor or a Manipulator ? Cannot figure it out from what little script you've posted

  9. #9
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Captain Proton
    You have to think of model, view and controller as abstract concepts. In one application there is never such a thing as 'one' model, one 'controller' or one view.

    Views can accept multiple 'models' and because model is such a broad and abstract concept, a View can accept a lot of things as.. well, model
    The more I work with the MVC pattern the more I start to *kind* of realize this. It's more the idea of MVC right?

    Thanks for you input!

    Matt

  10. #10
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Widow Maker
    Can you explain more...

    PHP Code:
    ...
    $view->setModel($user);
    ... 
    Exactly what does this method do ?

    Is it an Accessor or a Manipulator ? Cannot figure it out from what little script you've posted
    Hi! Actually I was just making a very basic example. Usually I just pass args to the views constructor as an associative array. In that array there may be a model, or two!

    matt

  11. #11
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I see Much the same way I do things really, then


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
  •