Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access controller method from inside a model

How do I access a controller method from inside a model?

like image 794
enchance Avatar asked Mar 05 '12 05:03

enchance


2 Answers

You don't.

Although it is technically possible, if you think that you need to, it suggests a flaw in your application's design.

The Controller layer is the backbone of you application and meant to handle requests from the user, talk to the Model layer, and stitch together the output in the View. Your Model layer should be blind to the Controller and View, but deal with data manipulation only. This is an over-simplified explanation of the MVC pattern (you can find resources for that elsewhere).

Your Codeigniter models should be reusable from any controller, and not dependent on them. There are many solutions to solve whatever problem it is that you have: You can pass data into a model in a number of ways, or you can use the result of a call to a model's method to perform an action in your Controller.

like image 181
Wesley Murch Avatar answered Nov 19 '22 05:11

Wesley Murch


You can use like this:

class some_model extends Model
{
   function getController()
   {
   $controllerInstance = & get_instance();
   $controllerData = $controllerInstance->getData();
   }
}
like image 28
Hareesh Avatar answered Nov 19 '22 06:11

Hareesh