CodeIgniter Forums
Call Controller from View - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Call Controller from View (/showthread.php?tid=16747)

Pages: 1 2


Call Controller from View - El Forum - 03-16-2009

[eluser]Amit Patel[/eluser]
Hell All,

How can i call controller function from view using CI.


Call Controller from View - El Forum - 03-16-2009

[eluser]Fero[/eluser]
why would you want to do that? why do you break MVC model?


Call Controller from View - El Forum - 03-16-2009

[eluser]cindor[/eluser]
Hi!

you shouldn't call controllers from views, it is against MVC pattern.


Call Controller from View - El Forum - 03-16-2009

[eluser]Amit Patel[/eluser]
Sorry for asking such question i know it's against from mvc pattern.

But i have one controller function which is call many times i know that i can defing that function in helper , library etc.. But for that i have to change my existing coding so how can i call controller function in any view file .

Sorry but i have to do that so plz help me if it possible


Call Controller from View - El Forum - 03-16-2009

[eluser]xwero[/eluser]
If you really don't want to change your code an iframe could be a solution. As the source you use the url leading to that function. But it's a very ugly hack.

Then there is the wick library that fetches the controller output for you. But it's not a good idea because views shouldn't be aware of where their content comes from. So you better load it in the controller method that gets called by the url and not in the view.

The best thing is to find out if that function really belongs to a controller but i guess not because you use it as a partial on other pages. And find a way to load the function for the controller methods that need it, i'm looking in the direction of hooks.


Call Controller from View - El Forum - 03-16-2009

[eluser]wiredesignz[/eluser]
Calling a controller method from a view is not worse than calling a library method.

The page controller functionality should be finished once the view begins rendering.

The MVC design pattern only states that a view contains enough logic to render itself and does not alter the application state.


Call Controller from View - El Forum - 03-16-2009

[eluser]Amit Patel[/eluser]
Thanks


Call Controller from View - El Forum - 03-16-2009

[eluser]xwero[/eluser]
I think the first sentence of wiredesignz needs to be extended to avoid confusion.
Quote:Calling a controller method from a view is not worse than calling a library method.
But it's best to not to call CI object methods in the views because they bind the view to CI.

This is why i made the remark in my response, that if you are going to use a controller method via the wick library you better do it in the controller than in the view.


Call Controller from View - El Forum - 03-16-2009

[eluser]wiredesignz[/eluser]
Xwero, There is always a direct relationship between a view and its controller, the view always relies on the controller to provide it with variables, objects or otherwise.

However the ONLY RULE is that the view must not alter the application state when accessing those variables or objects.

i.e. A view may access data stored in a model but it must not force the model to run a query to get that data, the controller should have done that and set the data up prior to view rendering.


Call Controller from View - El Forum - 03-16-2009

[eluser]xwero[/eluser]
[quote author="wiredesignz" date="1237229981"]i.e. A view may access data stored in a model but it must not force the model to run a query to get that data, the controller should have done that and set the data up prior to view rendering.[/quote]
Maybe i'm stretching the logic here but isn't calling a controller method a different way of running a query?

I agree with you about the only rule. But i'm thinking beyond the MVC pattern when i write it’s best to not to call CI object methods in the views because they bind the view to CI.
If you are using different frameworks you start looking for a way to create basic view files that you can drop in everywhere and in that view loose variables are better than CI object calls.
Even if you don't use different frameworks i think it's better to use loose variables to make the views futureproof.