Welcome Guest, Not a member yet? Register   Sign In
MVC pattern -> calling views direct from controller or from controller over a model?
#1

[eluser]F. Albrecht[/eluser]
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:

Code:
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:

Code:
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 Big Grin

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
#2

[eluser]wiredesignz[/eluser]
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
#3

[eluser]F. Albrecht[/eluser]
Thanks!

*reading*

Smile

2nd
#4

[eluser]Grahack[/eluser]
To me a model shouldn't be aware of the way it's displayed. Gonna read this page too anyway!
#5

[eluser]maadmac[/eluser]
[quote author="wiredesignz" date="1199586447"]Strictly speaking your first example is the better method.[/quote]

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.
#6

[eluser]Developer13[/eluser]
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!




Theme © iAndrew 2016 - Forum software by © MyBB