我在codeigniter中有一个类,它只处理ajax。我在这个类中创建了一个函数来检查refferal是否是一个ajax refferal,我想每次使用这个类中的任何函数时都能调用这个函数。
因此我实施了__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()
个。但它没有被调用(目前我的ajax请求仍然返回数据)这是解决问题的有效方法吗?
答案 0 :(得分:5)
只有在调用不可访问的方法时才会触发我希望能够打电话给我 功能每次都有功能 从这个类使用
__call。如果你希望每次调用一个控制器方法时都要执行某些功能(并且我认为你的意思是指一个映射到web请求的方法,而不是类中的任何函数),您应该将此功能放在构造函数中。
答案 1 :(得分:0)
如果您希望某个方法在任何方法之前运行,而不是在codeigniter控制器中使用魔术php __call,则可以对Codeigniter使用Remaping方法
public function _remap($method)
{
// call and start a method before any other method in codeigniter controller
}
有关更多信息,请参见this page