Welcome Guest, Not a member yet? Register   Sign In
MVC best practices, to display the models
#1

[eluser]Dready[/eluser]
Hello all,

I read some threads here about MVC best practices, on the subject of what part is responsible of displaying the model.

As I read I saw two "schools" :

* in the first one the view is responsible of displaying the model, and as so is "intelligent", which means the view embed PHP code.
* in the second one the controller is responsible of taking the model, extracting data, in general as an array, and then display with the help, for example of the parser class. In this case the view file has no PHP code, only {templates tags}.

On my own implementation of models, my models can return an array describing themselves and that can directly be used through the Perser class. To illustrate :

Code:
// somewhere in the controller
$myobject = new somethingModel($id);
$object_html = $this->parser->parse('some_view_file', $myobject->parser_array(), TRUE);

And of course, because things are not all that simple, I ended by adding a "template file" argument to the function, and so models can return their "string" output, be it in xml, html or whatever, directly :

Code:
// somewhere in the controller
$myobject = new somethingModel($id);
$object_html = $myobject->parser_array('some_view_file');

which means my models access to the parser class, some part of the view is managed by the controller (website layout, menus,...) and some other part by the model itself.

I'm asking you what do you think about this design, and what are the problems of doing like this ? Is it MVC-acceptable ?

Thanks for your opinions.
#2

[eluser]wiredesignz[/eluser]
Models represent the data source of an application. They must not access the View.
#3

[eluser]Dready[/eluser]
That's what I noticed readig other threads. But can models have a function to represent themselves as an array suitable to be used by the parser class, if the "$parser->parse" part is in a controller, as in my first example ?
#4

[eluser]wiredesignz[/eluser]
Yes of course, the data returned from a Model can be an array. The Model object itself can even be passed into a View as a variable if you so wish.
#5

[eluser]Sumon[/eluser]
[quote author="wiredesignz" date="1221947763"]The Model object itself can even be passed into a View as a variable if you so wish.[/quote]
wiredesignz, may i have an example how model object passed into view as a variable.

thanks in advanced.
#6

[eluser]Colin Williams[/eluser]
Code:
$this->load->model('some_model', 's_model'); // Model is now $this->s_model
$this->load->view('some_view', array('s_model' => $this->s_model));
#7

[eluser]Sumon[/eluser]
thanks Colin,
lets say s_model have 3 methods member_list(), blog_list(), total_post()
Code:
$this->load->view('some_view', array('s_model' => $this->s_model));
does the above line mean: from my view file (some_view.php) i am able to access member_list() method by
Code:
$s_model->member_list();
i think i am wrong. please let me know correct one and if you have a complete short example(MVC) that will be so nice for me Smile
Thanks in advance.
#8

[eluser]Dready[/eluser]
So you aknowledge that my first example is "MVC compliant" and my second one is not... ? Now all I want to know is what are the drawbacks of the second method ? Is this just because "you must not do that in MVC" or can this lead me to problems later in the development ?

You know I have nothing against "Models represent the data source of an application. They must not access the View." as soon as I understand why :-)
#9

[eluser]Colin Williams[/eluser]
Quote:i think i am wrong

You are only wrong about being wrong. That is exactly what you do. (You actually have a reference to the object, if we're being picky about terms.) You can access the object's properties and methods with $s_model from your view.
#10

[eluser]Colin Williams[/eluser]
Quote:“Models represent the data source of an application. They must not access the View.” as soon as I understand why

Models are responsible for fetching, structuring, and returning data from resources like databases, web services, and flatfiles. In what scenario do you see this needing a view file? The other way around, the View accessing the Model, is typically fine provided you aren't altering the state of the model (think of it as "read only" access).




Theme © iAndrew 2016 - Forum software by © MyBB