Jump to content

[SOLVED] MVC Help


LemonInflux

Recommended Posts

Hello. I've currently been looking into MVC for some software I'm writing. However, a couple things have stumped me.

 

What does each thing actually do? As I understood it, view took care of templates and layout, controller did content and dynamic elements, but then there's no model. So can someone give me a human definition for each?

 

Thanks in advance

Link to comment
Share on other sites

so if I have, say, a gallery page...

 

view would fetch the template, css, etc. etc.

model would collect the photos, info etc. etc.

controller would stick the two together?

 

Is that right?

The model is in fact "stupid" and cannot do any actions on other objects. Your view is your template. The model just describes what the element contains:

class photo{
  var $name;
  var $id;
  var $imageLocation;
}

class album{
  var $id;
  var $name;
  var $photos = array(); //Array of photo objects
}

class galleryController{
  public function getAlbum( $id ){}

  public function displayAlbum( $id ){
    $album = $this->getAlbum( $id );
    $template = new template();
    $template->assign( 'album', $album );
    echo $template->display( 'album.template.php' );
  }

  public function addPhotoToAlbum( $photo, $album) {}
}

Here you define two models: a photo and an album. The controller galleryController controls both of them (actually you need for the perfect design two controllers, but this is ok). The template 'album.template.php' is your view.

Link to comment
Share on other sites

If I may add my two cents, since I struggled with the same a few weeks ago.

 

After some reading and research I came to the conclusion that there are many "ways" to implement MVC. To name one for example, Codeigniter goes away with the model and integrates it into the controller, which is a departure from the traditional MVC implementation. The traditional way (and what I chose to use) is the "Fat model thin controller" approach.

 

The model contains all your application logic, all your procedures, database interactions, validation, data manipulation, preparing and delivering. The controller is just an elaborate wrapper over your model; deppending on what page you are visiting (controller action) it decides which model methods to call, what data to pass to them, what to do with the data returned, which template to render, where to redirect, etc. It "controls" the application flow.

 

I believe the controller should contain nothing more that very light validation (check if logged in for example) and simple control statements (if -then -else). The model is the true core of your application which spits data ready for your controller to pass to the View as-is with no further modifications. In fact, aside from setting a few variables with model or user input data and pass them around, the controller should touch nor manipulate no data at all. That's the model's job.

 

This way you can create for example a Gallery Model with methods such as fetchById(), addPictureToGallery(), removePictureFromGallery(), deleteGallery(), createSubGallery(), etc. The controller then calls this model methods with GET or POST arguments and the passes the results to the View.

If you then create another site which also uses a gallery, you can use the same model (and DB info most likely)... since it does not care where the data comes from it will work fine as long as you supply it in the correct format. But the new controller may get the arguments from POST, from SESSIONS, from another model, wherever.

 

Hope it helps. And if anyone has something to add or correct please do, I'm far from being an expert in MVC.

Cheers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.