帮助使用 __call 和 codeigniter
我在 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()
始终返回 false
和 exit()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
__call 仅在调用不可访问的方法时触发。如果您希望每次调用控制器方法时都执行某些功能(我假设您指的是映射到 Web 请求的方法,而不是类中的任何函数),您应该将此功能放在构造函数中。
__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.
如果您希望一个方法在任何方法之前运行,而不是在 codeigniter 控制器中使用 magic php __call ,您可以使用 codeigniter 的重新映射方法
以获取更多信息,请参阅 此页面
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
for more info see this page