Jump to content

Models and Views


proggR

Recommended Posts

In my last project I had the controller doing more than a lot of people make controllers do. It took the request, made basic checks (check that an id is specified if its required, used the SessionModel to make sure the user is logged in), then made the call to the appropriate model(s) and finally, instantiated the view with various modules (my term for them, basically a representation of specific information in HTML). The models handled form validation and whatnot via a Validation class since they have a better understanding of what each bit of information they are expecting in each case would be.

 

With my current project, would it be better to have these modules(read as views) make the requests of the model themselves as is the norm with MVC design? The project is music related so lets say a module(view) was supposed to be the html used to display information for an album. Would it be best to have the album_info module call Album::getById()? Or is it acceptable practice to have the controller do something like $album = Album::getById(); $this->ui->addModule(Modules::album_info($album)); In the former example I would still need to instantiate the module and add it to the ui class, the difference is where the information is being retrieved.

 

 

Also that isn't exactly how the code would be written but its close enough for the examples. I'm positive there are tons of unconventional things with my approach and I welcome any feedback if anything seems wrong to anybody. I'm looking to build this application better than I have built other ones since it's one I'm actually excited to work on for a change (the rest have more or less been practice/application style projects).

 

Another quick question (may as well get them all out now). What is the standard approach for models requesting information from other models. To stick with the album example, the Album model has all the information specific to the Album, including an array of songs which are represented by a Song model. Is it appropriate to have a function within the Song model similar to ::getAllForAlbum() that can be called from the Album model to return an array for the particular album? Also, as far as the relation to the band is concerned, would the Album also have an instance of the Artist or the Artist instances of Album (which doesn't seem right to me).

 

As I'm sure you can tell I haven't used MVC too much and I've been teaching myself but seem to have confused terms and practices along the way. Hoping to get all that sorted out. Also I'm aware frameworks exist to alleviate a lot of this headache but I like to work from my own codebase as much as possible.

 

Thanks in advance for any help. Sorry if I'm way off base with all this.

Link to comment
Share on other sites

The Controller belongs in the Application Layer this means that it knows everything Application specific like POST and GET. So in your Controller you would write:

 

$album = new AlbumRepository();
$this->ui->assign('album', $album->find($_REQUEST['id']);

 

Avoid the use of static as much as possible. It's OO not procedural.

Link to comment
Share on other sites

The AlbumRepository being the model in this case? And is the assign pairing the values within the model with the view labeled album? I think I understand what you mean. The code is much cleaner the way you have it. Thanks for that.

 

*edit*

Also, I'm not sure why I wrote the example as if it were static since I have no static functions in my code :\

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.