帮助使用 __call 和 codeigniter

发布于 2024-10-03 17:11:54 字数 594 浏览 1 评论 0原文

我在 codeigniter 中有一个类专门处理 ajax。我在此类中创建了一个函数,用于检查引用是否是 ajax 引用,我希望每次使用此类中的任何函数时都能够调用此函数。

因此,我已经实现了 __call 魔术方法

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);

        }

,目前 ajaxCheck() 始终返回 falseexit() s。但它没有被调用(目前我的ajax请求仍然返回数据)这是解决问题的有效方法吗?

I have a class in codeigniter which deals solely with 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.

As such I've implemented the __call magic method

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. But it's not being called (at present my ajax request still returns data) Is this a valid way of approaching the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Saygoodbye 2024-10-10 17:11:54

我希望能够调用这个
每次执行任何函数时都函数
使用此类中的

__call 仅在调用不可访问的方法时触发。如果您希望每次调用控制器方法时都执行某些功能(我假设您指的是映射到 Web 请求的方法,而不是类中的任何函数),您应该将此功能放在构造函数中。

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. 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.

恋竹姑娘 2024-10-10 17:11:54

如果您希望一个方法在任何方法之前运行,而不是在 codeigniter 控制器中使用 magic php __call ,您可以使用 codeigniter 的重新映射方法

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

以获取更多信息,请参阅 此页面

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

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

for more info see this page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文