CodeIgniter Forums
calling functions from the view file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: calling functions from the view file (/thread-11330.html)



calling functions from the view file - El Forum - 09-04-2008

[eluser]jrlooney[/eluser]
Kinda new to CI and not sure why this doesn't work. If my controller looks like (not all functions shown - just the ones necessary for this example):

Code:
class Contact extends Controller
{
    private $uri_params = array();

    function Contact()
    {
        parent::Controller();
    }
    function get_sort_link($value)
    {
        $this->uri_params['sort'] = $value;
        return $this->uri->assoc_to_uri($this->uri_params);
    }
}

Why can I not do the following in the view file:

Code:
$this->get_sort_link('first_name');

This results in:

Fatal error: Call to undefined method CI_Loader::get_sort_link() in /Users/php/ci/contact_manager/views/contact_list_view.php on line 15


calling functions from the view file - El Forum - 09-04-2008

[eluser]Sarfaraz Momin[/eluser]
You cannot call a controller method from the view. Instead you can call them in the controller and pass the value that it returns to the view. It does not make sense to do such a thing in MVC. Smile

Have a good day !!!


calling functions from the view file - El Forum - 09-04-2008

[eluser]jrlooney[/eluser]
Thank you for the reply. I had that feeling, MVC is definitely something I have to get used to (though I want to). So at runtime, all processing is done in the Models and Controllers and View is truly only meant to display? Gotta drill that in my head. Smile


calling functions from the view file - El Forum - 09-04-2008

[eluser]Sarfaraz Momin[/eluser]
Yes you got it right this time. People still tends to make it work the way they want by making modifications in the core. It does not make sense to do it. CI gives u so much flexibility with libraries, helpers and hooks that you can use them to achieve all of thet tasks. Controllers are purely business logic (processing), Models for database activities and view for displaying them.

-Sarfaraz


calling functions from the view file - El Forum - 09-04-2008

[eluser]phpoet[/eluser]
I was actually just engaged in a long discussion about the responsibilities of the different parts of CI's MVC framework. A traditional MVC framework usually has all the business logic in the models rather than the controllers. Controllers are used to control the flow of data between models and views. The sort of funny thing about the way CI handles models is that all models are singletons - representing a "type" of data rather than an instance of data. Depending on what the need is, you may find it helpful to use your own Library classes in the way that you might use Models in a more traditional MVC framework.


calling functions from the view file - El Forum - 09-04-2008

[eluser]sophistry[/eluser]
or you could check out Modular Extensions ME which implements Hierarchical MVC (HMVC) in CI. see wiki page for quick start.