Part of the EllisLab Network
   
 
MVC pattern -> calling views direct from controller or from controller over a model?
Posted: 05 January 2008 03:11 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  137
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 03:27 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2634
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 - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

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

Thanks!

*reading*

smile

2nd

 Signature 

ideenhafen® | creativemac

Profile
 
 
Posted: 05 January 2008 05:01 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  493
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 07:46 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  305
Joined  04-26-2007
wiredesignz - 05 January 2008 03: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 08:49 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  548
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
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120301 Total Logged-in Users: 47
Total Topics: 126418 Total Anonymous Users: 4
Total Replies: 664836 Total Guests: 450
Total Posts: 791254    
Members ( View Memberlist )