Welcome Guest, Not a member yet? Register   Sign In
simple question
#1

[eluser]vile[/eluser]
can i use controller's function in view pages?
#2

[eluser]flojon[/eluser]
No, and that would also break the MVC structure.
One solution is to instead put the function you want to share between controller and views inside a library or helper.
#3

[eluser]Colin Williams[/eluser]
Quote:No, and that would also break the MVC structure.

I do agree that it's a bad approach that can be handled much more elegantly with enough forethought, but I'm done saying stuff like "this breaks the MVC structure." MVC is a pattern that is largely open to interpretation. You can find MVC patterns applied to anything, real or virtual. CI's convention is that Controllers react to requests defined by the URI. Others argue that a controller is just something that reacts... to anything (which means that any function or method is in some respect a Controller). Same applies to Models. Anything that manipulates and/or retrieves data is a Model (so, for instance, one can argue that the Config class is a model for your configuration settings).

To call controller methods from anywhere in your app, search the Wiki for "Wick" and "Modular Extensions" (pardon the misleading nomenclature of that latter one... it's more like "Modular Organization" or "Modular Behavior"). They both provide a syntax for calling controller methods in many scopes.
#4

[eluser]vile[/eluser]
thanks! it worked! by the way how to i use db class in my own library?
thanks
#5

[eluser]Colin Williams[/eluser]
Read the user guide, for one. You can get an instance of the CI object with the get_instance() function

Code:
class Library {

  var $ci;

  function Library()
  {
     $this->ci =& get_instance();
  }

  function do_something_with_db()
  {
    $query = $this->ci->db->get();
  }

}
#6

[eluser]vile[/eluser]
thank u very much!
#7

[eluser]m4rw3r[/eluser]
I usually only load the db object into $this->db.
It gets so messy with the whole CI object when i dump the library object.




Theme © iAndrew 2016 - Forum software by © MyBB