• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling functions from the view file

#1
[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

#2
[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 !!!

#3
[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

#4
[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

#5
[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.

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.