Part of the EllisLab Network
   
 
MVC pattern -> calling views direct from controller or from controller over a model?
Posted: 05 January 2008 04:11 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  139
Joined  03-20-2007

Hi,

after 6 successful projects with exciting ExpressionEngine I’m turning to CI to getting deeper in that MVC thing smile

I’ve just played a little bit with the framework last days and read the docu. Yesterday I’ve got the book (David Upton) and read some pages. Therefore I have a question:

In my understanding the MVC pattern is working as this:

Class My_controller

    this
->load->my_model
    data 
this->my_model->getdata()
    
this->load->my_view(data)

Class 
My_model

    
function getdata() 


In the book the view call happens via an indirect way like this:

Class My_controller

    this
->load->my_model
    this
->my_model->display()

Class 
My_model

    
function display()
        
data this->db->query(anything)
        
this->load->my_view(data


Sorry for my simplified syntax :D

I would prefer the first snippet but I don’t know whether it is in the straightfoward MVC theorie? Or does it no matter?


Thanks,

2nd

 Signature 

ideenhafen® | creativemac

Profile
 
 
Posted: 05 January 2008 04:27 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3198
Joined  06-10-2007

Strictly speaking your first example is the better method.

See here for a good explanation of MVC.
http://www.phpwact.org/pattern/model_view_controller

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 05 January 2008 04:30 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  139
Joined  03-20-2007

Thanks!

*reading*

smile

2nd

 Signature 

ideenhafen® | creativemac

Profile
 
 
Posted: 05 January 2008 06:01 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  297
Joined  02-21-2007

To me a model shouldn’t be aware of the way it’s displayed. Gonna read this page too anyway!

Profile
 
 
Posted: 05 January 2008 08:46 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  04-26-2007
wiredesignz - 05 January 2008 09:27 PM

Strictly speaking your first example is the better method.

Agreed.  As a general rule, you want to do all the db calls (via model) and output (via view) through the controller, which acts as “traffic” cop.  CI doesn’t even require models, you can actually do all your data processing in the controller… but it’s good practice to get in the habit, anyway.  As mentioned above, you want to decouple the model and view.

 Signature 

OnWired LLC | EE Pro Network Member

Profile
 
 
Posted: 05 January 2008 09:49 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  647
Joined  02-25-2007

That’s the biggest problem that I had with Upton’s book was the fact that he gave his models output responsibility.  No no no!

 Signature 

MyClientBase - Free and Open Source Client and Invoice Management

Profile