SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MVC: Where do things go?

    Hi guys,

    I am building my first MVC system, and I feel I am starting to get the hang of it, slowly...

    My problem is that I am starting to wonder if I am putting certain things in the correct place. At the moment my application works as follows:

    • FrontController (index.php) delegates to a PageController and Action based on the query string. For instance if I have www.domain.com/members/add, my routing function calls the members_controller and runs the add function.
    • The PageController instantiates a model and performs any necessary model function calls.
    • The PageController then instantiates a view and runs a function on the view which will inevitably display the data.

    I have a feeling though that I am missing something, as the way I have built my application feels, well, clunky.

    First of all my model seems to continually need functions added to them, and I think they are in the wrong place. How do you handle things like adding data to the database, or updating records? Should the model handle that? At the moment I am doing:
    PHP Code:
    $model = new PhotosModel($this->registry);
    $model->addRecord($record);
     -- or --
    $model->updateRecord($record); 
    When the model is created it loads the correct data from the database, but I just don't know if making modifications to the data, sending changes back, and that sort of thing are best handled by the model? Should the controller itself handle these things, or am I way off track with my thinking?

    Also I find I am reproducing code for all of my view functions. Such as:
    PHP Code:
    $template->setTitle($title);
    $template->printHeader(); 
    And so on, is this normal? I mean I have about 15 views currently within two view objects, and they all replicate the same commands mostly. Is there a better way of managing this that I am not aware of?

    Thanks a lot for your time guys, I am really enjoying learning these new techniques and am finding it fits my programming style perfectly.

    Kind regards,
    Jordan

  2. #2
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anybody able to give any advice please?

  3. #3
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    May I ask what your model represents? Does it incorporate all domain model interaction in your application?
    I always think of MVC as (normally) one controller, several views and a model which consist of interacting domain objects.

    Your domain objects could be the receiver of create/update/delete statements but not necessarily handle this themselves. Maybe you can create a separate class which handles all domain object's db operation.


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
  •