Welcome Guest, Not a member yet? Register   Sign In
What goes where? (MVC)
#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".


Messages In This Thread
What goes where? (MVC) - by El Forum - 04-09-2009, 11:30 AM
What goes where? (MVC) - by El Forum - 04-09-2009, 11:40 AM
What goes where? (MVC) - by El Forum - 04-09-2009, 12:03 PM
What goes where? (MVC) - by El Forum - 04-09-2009, 12:29 PM
What goes where? (MVC) - by El Forum - 04-09-2009, 01:26 PM
What goes where? (MVC) - by El Forum - 04-09-2009, 01:42 PM
What goes where? (MVC) - by El Forum - 04-09-2009, 01:50 PM
What goes where? (MVC) - by El Forum - 04-09-2009, 01:57 PM
What goes where? (MVC) - by El Forum - 04-09-2009, 02:07 PM
What goes where? (MVC) - by El Forum - 04-09-2009, 02:08 PM
What goes where? (MVC) - by El Forum - 04-09-2009, 03:59 PM
What goes where? (MVC) - by El Forum - 05-22-2009, 10:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB