views:

103

answers:

6

I've been using the CodeIgniter framework for PHP and am enjoying it, but I notice that it seems to require a controller for every view. I'm wondering if there is a way to call a specific model from the view itself, rather than route through a controller. I understand that use of a controller is best practice in most cases, especially where the data from the model needs to be modified in some way, but I have cases where I just need to do a strict data pull to the view (which is loaded via ajax), and setting up a controller for that seems superfluous.

Any thoughts? Thanks in advance!

A: 

I'm wondering if there is a way to call a specific model from the view itself, rather than route through a controller.

That's not possible as of what I know, the main abstract class of the CI controller imposes restriction to use a controller otherwise you will get a fatal error.

And actually what you say will break the best practice of MVC design pattern. You got to go to model through a controller not view.

Sarfraz
*And actually what you say will break the best practice of MVC design pattern.* It all depends on which "version" of MVC you adhere to. There are at least 3 different "versions"... One says the Controller pushes the model into the view. One says the View pulls the model into itself. And another says the controller pushes the data into the view (So the model is 100% separated from the view with no dependencies). While I do agree with you that the most important thing is to be consistent, it's not breaking MVC to hook things up in a different order. MVC is about separation, not relation...
ircmaxell
@ircmaxell: It is interesting that there exists three versions of it, i was talking about pattern CI adheres to. Would be great if you could post a link where those three versions are discussed, thanks :)
Sarfraz
A: 

I'm a bit confused as to exactly what you're trying to achieve. The controller's value, aside from just being a clean way to handle incoming requests, is to manage the interaction between the models and the views and to determine which views to load. It's also entirely reasonable to load model data directly from your views, but how did you get to your view in the first place?

I guess I'm just having a hard time seeing the context here..

treeface
A: 

To run a query via Ajax you still need to provide a URL / path in the javascript call. You can not get around the fact that a controller function has to "catch" this call; you can not map a url directly to a model. All you need is 3-4 lines of code in your controller.

Via URI routing you can map a URL to a different controller, so you don't "require a controller for every view". I always create a controller called "ajax" to handle those requests.

A basic ajax call with jquery can be something like this

$('#prod_img').load( "http://domain.com/ajax/get_img", {'color': 'blue', 'url_title': 'bla' } )
stef
+1  A: 

You're fundamentally misunderstanding MVC, at least as implemented in CI.

All URLs on your site (at least those that utilize the CI framework) are mapped to functions (methods) within controllers.

http://myCIsite.com/controller/method[/var1][/var2]...

It doesn't matter whether the URL is accessed via regular HTTP or via AJAX. This is always a one to one mapping. Because of this, you should think of the controller/method combination as the "web page". Do not think of the view as the web page.

Models and views are subordinate to controllers. The controller delegates specific responsibilities to them - database interaction for models, and page output to views.

Because models and views only serve to perform delegated responsibilities, their use is not required in any given controller/method. Help pages, for example, generally have no need to interact with a database, so there is no model utilized by the controller/method combination that serves a given help page. Likewise, form handlers frequently redirect to another page upon completion of processing. As such, there is no view corresponding to the form handler (but there is (likely) a view called from the controller/method in the redirected to page).

Furthermore, models and views do not necessarily correspond on a one to one basis with individual controllers/methods. Any given model can be loaded and used from within several controllers. Similarly, a controller could have a single monolithic view that is used by all methods, or each method could be assigned its own view. (Or, as I just said, a given controller/method could utilize no view at all.)

Finally, CI does not enforce strict MVC separation. You can interact with the database and echo HTML all from within the controller and CI will not complain. Nevertheless, this separation and delegation of responsibility is followed because logically separating the responsibilities makes the code easier to read and helps you follow the DRY principle in your coding.

The fundamental Understanding is that the "web page" corresponds to the controller/method. The view and controller, when used, handle delegated responsibilities for the controller/method.

coolgeek
A: 

You can echo stuff in your controller, so rather than trying to bypass the controller you should be looking into how to do away with the views. This will actually be easy, you can load the db class in the controller just as you can in a model.

But if you really don't want to use MVC perhaps Codeigniter is not the framework for you.

Calle
Seems like coolgeek already told you this, but I didn't read his full answer at first. Mine is shorter anyway (but his better) :)
Calle
A: 

You should read more into the principles of MVC. Views are strictly for presentation of data, the model shouldn't communicate with views directly.

But still if that's what you want then just pass $this->db from the controller to the view and use it in the view. Then again, this is NOT a good practice.

feketegy