Access controller method from inside a model

前端 未结 2 780
借酒劲吻你
借酒劲吻你 2020-12-20 17:42

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

相关标签:
2条回答
  • 2020-12-20 18:24

    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.

    0 讨论(0)
  • 2020-12-20 18:27

    You can use like this:

    class some_model extends Model
    {
       function getController()
       {
       $controllerInstance = & get_instance();
       $controllerData = $controllerInstance->getData();
       }
    }
    
    0 讨论(0)
提交回复
热议问题