![]() |
Calling a public controller method from a view? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Calling a public controller method from a view? (/showthread.php?tid=4174) Pages:
1
2
|
Calling a public controller method from a view? - El Forum - 11-11-2007 [eluser]europe72[/eluser] Still a newbie with CI... I have a controller named A In controller A I have a method named Foo In a view, I want to call method Foo from controller A How is this done? If I try $A = new A(); $A->Foo(); PHP errors out on me Thanks Calling a public controller method from a view? - El Forum - 11-11-2007 [eluser]gtech[/eluser] I don't tend to call controllers in the view code, its not really designed to do that.. if you are using javascript you can make ajax calls, but im guessing thats not what you are doing? What I would do is call the fucntion Foo, from inside your controller and then pass that information to the view. Calling a public controller method from a view? - El Forum - 11-11-2007 [eluser]europe72[/eluser] So then are you saying it can't be done? Other posts seem to suggest a library, so maybe I have to go that route. If it can be done though I would like to know. Thanks. Calling a public controller method from a view? - El Forum - 11-11-2007 [eluser]gtech[/eluser] well lets put it this way, its not that it can't be done. its that its not a nice way to do it (other people may have a different opinion).. If you want you can post your code up and I can take a look for you, im between decorating at the mo ![]() [edit] after a bit more knowledge you should not call a controller function from a view. [/edit] Calling a public controller method from a view? - El Forum - 11-11-2007 [eluser]europe72[/eluser] Library just seems easier...I'll just do that thanks Calling a public controller method from a view? - El Forum - 11-11-2007 [eluser]gtech[/eluser] ok good luck Calling a public controller method from a view? - El Forum - 11-12-2007 [eluser]Base Willy[/eluser] I've aksed the same question a little time ago and we decided that the calling a controller from the view isn't right and it's better to use models Calling a public controller method from a view? - El Forum - 11-14-2007 [eluser]marcoss[/eluser] [quote author="europe72" date="1194818427"]Still a newbie with CI... I have a controller named A In controller A I have a method named Foo In a view, I want to call method Foo from controller A How is this done? If I try $A = new A(); $A->Foo(); PHP errors out on me Thanks[/quote] If you want to call a function defined in your controller within a view file, remember you are within the same object, so just call the method like $this->foo();, if you need a method from a model, you would go with $this->model->foo(); (assuming the model is loaded already). However, i would advice you not to use model methods within views, the ones form the controller are fine, but still the best option would be to create a helper that encapsulates the actual method. Calling a public controller method from a view? - El Forum - 11-14-2007 [eluser]wiredesignz[/eluser] Quote:However, i would advice you not to use model methods within views, the ones form the controller are fine, but still the best option would be to create a helper that encapsulates the actual method. Totally backward. Controllers represent the entry point and logic for a View whereas the Model represents the information for the View to display... Views SHOULD use Model methods to obtain information not Controller methods. MVC divides an application into three concerns: Model - Encapsulates core application data and functionality Domain Logic. View - obtains data from the model and presents it to the user. Controller - receives and translates input to requests on the model or the view. The separation into three concerns is inspired by a information processing model where the controller represents system input, the model represents processing and the view represents the output of the system. Calling a public controller method from a view? - El Forum - 11-14-2007 [eluser]marcoss[/eluser] [quote author="wiredesignz" date="1195098611"] Quote:However, i would advice you not to use model methods within views, the ones form the controller are fine, but still the best option would be to create a helper that encapsulates the actual method. Totally backward. Controllers represent the entry point and logic for a View whereas the Model represents the information for the View to display... Views SHOULD use Model methods to obtain information not Controller methods. MVC divides an application into three concerns: Model - Encapsulates core application data and functionality Domain Logic. View - obtains data from the model and presents it to the user. Controller - receives and translates input to requests on the model or the view. The separation into three concerns is inspired by a information processing model where the controller represents system input, the model represents processing and the view represents the output of the system.[/quote] Read my signature, i won't do it. So, just to illuminate you a little bit, the controller takes the user input, then asks the model what to do, the wise model answers and then the controller takes that raw data and passes it over the view, which is nothing more than a presentational element. So just in the case you need to apply some sort of complex formating to that data you would use a helper function or method, that kind of functionality is not supposed to be in the model, nor in the controller it is just a one purpose function, isolated... a helper. Period. |