Welcome Guest, Not a member yet? Register   Sign In
What goes where? (MVC)
#1

[eluser]SomeFunkyDude[/eluser]
Hello,

I just am a little confused as to what should go where, so far I'm reading that....

model - functions that work with/retrieve/update info from the database
controller - functions that do calculations? or image resizing? store cookies, etc? and also loads appropriate views/css/javascript files
view - presentational layout

I noticed in one tutorial on making a CI app, Derek Allard used the model to slightly format database results into an unordered list, should mild/extensive data formatting be done in the model?

Thanks.
#2

[eluser]TheFuzzy0ne[/eluser]
This is how I see it:

Model
Models are used almost as if it's a database itself. It's a layer between your application and your data storage medium, be it flat files, a database, XML files etc... Your models facilitate your application's handling of stored data.

View
Views are used to format your output before displaying it to the user. Views don't have to contain HTML, but often contain simple PHP logic such as IF statements and loops.

Controller
Controllers are the API for your user. It's the only point that a user can access your application, and serves as the glue between views, models, libraries helpers etc... A controller is analogous to how telephone operators used to work when phones were first invented. They connect the calls to where they need to go.

Hope this helps.

EDIT: This thread may interest you: http://ellislab.com/forums/viewthread/107773/
#3

[eluser]RJ[/eluser]
Great thread suggestion
#4

[eluser]richthegeek[/eluser]
Just to expand on TheFuzzyOne's post;

The Model does most of the heavy lifting - it should contain methods that do almost everything to do with that "object". For example, in a store there could be a "Product" model, which when relates to a single product. There would be methods for retrieving the product info, any related products, its stock levels and so on, as well as methods for creating, updating, and deleting a product to the database.

The Controller links between the Model(s) and the View(s) - on a specific page, a perfect controller method will only call a bunch of model's methods into a data array and pass it to a view or three.

To make this absolutely clear: YOU SHOULD NOT DO ANY HEAVY LIFTING IN YOUR CONTROLLER! The controller should just say "I need that info, get it from the model".

If that info needs to sorted, or modified even extensively, all that work should be done through a model or series of models, not by the controller.


So, an example (theoretical) Controller method for a e-commerce store;

Code:
/* Product.php */

function view( $id = false )
{
    if( !$id ) redirect( "home" );
    
    $product = $this->product->get_all_data( $id );
    $user = $this->user->get_user_info();
    $cart = $this->cart->get_cart_array();
    $categories  = $this->categories->get_children( 0 ); /* imagine the 0 is the root node in a modified pre-order tree setup, we might instead use the main category of $product */
    
    $data = array( "product"=>$product, "user"=>$user, "cart"=>$cart, "categories"=>$categories );

    $this->load->view( "site/header", $data );
    $this->load->view( "product/listing", $data );
    $this->load->view( "site/footer", $data );
}

That is all entirely theoretical, but it shows an "ideal" controller method in that case - there is no data processing, just "i need this, get it, show it in these views".
#5

[eluser]Colin Williams[/eluser]
Great overview, FuzzyOne.

I've always liked the Controller as Traffic Cop idea. My take: http://ellislab.com/forums/viewreply/452130/
#6

[eluser]SomeFunkyDude[/eluser]
OK, it's starting be a little clearer for me now, the Controller is JUST the traffic cop, any classes/functions/processing/logic/database processing, etc. should be in the model, the view should mostly be html, basic data displaying templates, or simple loop or if/else statements.

On a side note...
[quote author="richthegeek" date="1239319769"]
function view( $id = false )
{
if( !$id ) redirect( "home" );

$product = $this->product->get_all_data( $id );
$user = $this->user->get_user_info();
$cart = $this->cart->get_cart_array();
$categories = $this->categories->get_children( 0 ); /* imagine the 0 is the root node .[/quote]
Can you assign the Boolean 'false' to $id in the method's arguments, and wouldn't that always make if(!$id) return false thus always redirecting to the home page.

Also, in the line:
Code:
$product = $this->product->get_all_data($id)
this wouldn't work unless the controller also loaded a model that has property of 'product' and method of 'get_all_data()' right?
#7

[eluser]RJ[/eluser]
First you have to load the model-
Code:
$this->load->model('my_model');
// note leave out the .php

This will load the model class and constructor.

After loading the model now we access a method in the model by:
Code:
$this->my_model->my_method();
#8

[eluser]TheFuzzy0ne[/eluser]
[quote author="Colin Williams" date="1239323216"]I've always liked the Controller as Traffic Cop idea. My take: http://ellislab.com/forums/viewreply/452130/[/quote]

Very interesting story. It sounds all too familiar. My girlfriend, she's a model, she went to the library and never returned. (Just kidding, of course, she went bowling and never returned...).
#9

[eluser]TheFuzzy0ne[/eluser]
[quote author="SomeFunkyDude" date="1239324143"]OK, it's starting be a little clearer for me now, the Controller is JUST the traffic cop, any classes/functions/processing/logic/database processing, etc. should be in the model, the view should mostly be html, basic data displaying templates, or simple loop or if/else statements.[/quote]

I wouldn't go as far as to say all processing and logic must be a model. Obviously you can use libraries, plugins and helpers too.

[quote author="SomeFunkyDude" date="1239324143"]Can you assign the Boolean 'false' to $id in the method's arguments, and wouldn't that always make if(!$id) return false thus always redirecting to the home page.[/quote]

FALSE is only assigned by default, if the argument provided is NULL (not specifying an argument throws NULL in by default). If NULL is not provided as the argument, then the supplied value is stored. Setting the default value is a great way to stop those errors you see when you don't supply an argument to a function:

Code:
echo date();

# Produces Warning: date() expects at least 1 parameter, 0 given in ./system/- on line 2

But then again, sometimes this behaviour is desirable.

[quote author="SomeFunkyDude" date="1239324143"]Also, in the line:
Code:
$product = $this->product->get_all_data($id)
this wouldn't work unless the controller also loaded a model that has property of 'product' and method of 'get_all_data()' right?[/quote]

Yes, but the model can be auto-loaded or loaded in the controllers constructor. Don't forget, that example was fictitious, although syntactically sound.
#10

[eluser]richthegeek[/eluser]
The $id = false bit assigns a defualt value.

In the case of this (relative) URL:
/product/view/13
it would get product with ID 13 (as $id = 13)

In the case of this:
/product/view
$id would deafult to false and redirect, as its a malformed URL.




Theme © iAndrew 2016 - Forum software by © MyBB