简体   繁体   English

使用Codeigniter帮助使用__call

[英]Help using __call with codeigniter

I have a class in codeigniter which deals solely with ajax. 我在codeigniter中有一个只处理ajax的类。 I have created a function within this class which checks if the refferal is an ajax refferal, I want to be able to call this function every time that any function from this class is used. 我在此类中创建了一个函数,该函数检查引用是否是ajax引用,我希望每次使用此类中的任何函数时都能调用此函数。

As such I've implemented the __call magic method 因此,我实现了__call magic方法

class Ajax_content extends Controller {

    function __construct()
    {
        parent::Controller();


    }

    function __call($method, $arguments){

            $this->ajaxCheck(); //set up to return false and exit.
            call_user_func_array(array($this,"_".$method),$arguments);

        }

At present ajaxCheck() always returns false and exit() s. 目前, ajaxCheck()始终返回falseexit() But it's not being called (at present my ajax request still returns data) Is this a valid way of approaching the problem? 但是它没有被调用(目前我的ajax请求仍然返回数据)这是解决问题的有效方法吗?

I want to be able to call this function every time that any function from this class is used 我希望每次使用此类中的任何函数时都能调用此函数

__call is only triggered when invoking inaccessible methods. __call仅在调用不可访问的方法时触发。 If you want certain functionality to be executed every time a controller method is called (and by that, I assume you mean a method that's mapped to a web request, not any function in the class), you should put this functionality in the constructor. 如果您希望每次调用控制器方法时都执行某些功能(因此,我认为您的意思是映射到Web请求的方法,而不是类中的任何函数), 则应将此功能放入构造函数中。

If you want a method to run before any method instead of use magic php __call in codeigniter controller you can use Remaping method for codeigniter 如果您希望某个方法在任何方法之前运行,而不是在codeigniter控制器中使用magic php __call,则可以对Codeigniter使用Remaping方法

    public function _remap($method)
    {
        // call and start a method before any other method in codeigniter  controller
    }

for more info see this page 有关更多信息,请参阅此页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM