Interacting with Controllers
You can call libraries, models, plug-ins, or helpers from within any controller, and models and libraries can also call each other as well as plug-ins and helpers.
However, you can't call one controller from another, or call a controller from a model or library. There are only two ways that a model or a library can refer back to a controller:
Firstly, it can return data. If the controller assigns a value like this:
and the function is set to return a value, then that value will be passed to the variable $fred inside the controller.
Secondly, your model or library can create (and send to a view) a URL, which allows a human user to call the controller functions. Controllers are there to receive human interactions.
You can't, of course, hyperlink directly to a model or library. Users always talk to controllers, never to anything else—but you can write a calling function in the controller. In other words, your view might contain a hyperlink to a controller function:
echo anchor(start/callmodel, Do something with a model); but the callmodel function would exit only to call a function in the model:
function callmodel() {
$this->load->model(mymodel); $this->mymodel->myfunction();
It's Just Like an Egg-Cup
This diagram shows the different ways in which components can address each other. Unbroken lines represent direct function calls such as:
$this->mymodel->myfunction();
These can take place from controllers to views, and from controllers to libraries or models. (Models can also call views, but probably shouldn't.) They can't take place in reverse: views etc. can't call controllers. However, libraries and models can call each other and be called by each other.
Broken lines represent passing information by returning values. Models and libraries can do this to controllers, or to each other. Views don't return values.
The dotted lines represent passing information or control via a human user—in other words, the view will show the user something on the screen and may invite the user to click on a hyperlink (which sets off a controller).
Any resemblance to an egg-cup is purely coincidental. It just came out that way.
Post a comment