Welcome Guest, Not a member yet? Register   Sign In
Best MVC design practice?
#2

[eluser]mddd[/eluser]
Welcome to Codeigniter and to the forums.
My first tip would be to read the user guide entirely (at least the General Topics area).

Then, on to your questions:

1. Personally I think calling models from views is ok, but only if you do it to check properties from the model. For instance, if you have a model that deals with user information, you could have if ($user_model->is_logged_in()) { // show something } in the view. Of course you could get that information in the controller and then send it to the model, but for this kind of 'read only' operation I would say it's ok to call the method from a view. But it would be more strict MVC to use the controller as a 'middle man'.

2. No. The controllers are meant to give structure. They split up the application into manageable parts! And in CI, there is a large part of automation in the controller phase: depending on the called url, a certain method in a certain controller is loaded. So the question 'where are we in the application' is answered in this phase.

3. No. Why would there be? Models are meant to be split up by subject. If you have a site with users, posts, news articles etc., you could make a model for each of those. Then you would call a model using $this->user_model->add() or $this->blog_model->list_recent() etc. Again: the whole point is to make things managable. If you put everything on one pile, you might as well build your entire application by including a file containing 100 functions!

4. Using libraries is a good practice. In CI, we have models which are libraries of functions around a part of your application, and libraries which are more generic. They usually contain a group of functions that take care of some task that relates to the application but is not the core of it. Libraries could be re-used in other applications.

Generally: try to only do output in a view. Use the controller as the 'general manager' of the project. Use the model as the 'data collector' that gets all the needed information. Use the view only to output things. Use libraries as 'specialists' that do specific tasks.


Messages In This Thread
Best MVC design practice? - by El Forum - 08-24-2010, 07:49 AM
Best MVC design practice? - by El Forum - 08-24-2010, 08:10 AM
Best MVC design practice? - by El Forum - 08-24-2010, 08:11 AM
Best MVC design practice? - by El Forum - 08-24-2010, 09:29 AM
Best MVC design practice? - by El Forum - 08-24-2010, 10:14 AM
Best MVC design practice? - by El Forum - 08-24-2010, 10:31 AM
Best MVC design practice? - by El Forum - 08-24-2010, 10:40 AM
Best MVC design practice? - by El Forum - 08-24-2010, 10:44 AM
Best MVC design practice? - by El Forum - 08-24-2010, 10:48 AM
Best MVC design practice? - by El Forum - 08-24-2010, 12:10 PM



Theme © iAndrew 2016 - Forum software by © MyBB