我正在使用CodeIgniter进行一个项目。我用自定义的MY_Controller扩展了CI的基本控制器类。MY_Controller具有认证标志变量$auth = FALSE
。在需要身份验证的页面上,我调用我的auth_model->runAuth()
函数来运行检查,如果所有检查都通过,则此标志应更新为TRUE
。由于某些原因,我无法使用$this->auth = TRUE
直接从auth_model更新my_Controller中的$auth
变量,但我必须先将检查结果传递回页面控制器,然后更新my_Coontroller的$auth
变量。如何在不经过控制器的情况下直接从模型更新MY_Controller中的$auth
标志?提前非常感谢!
您最好的选择是通过类似的方法调用直接分配标志
$this->auth = $this->auth_model->runAuth();
在MY_Controller类中!方法runAuth()
不需要大的改变:
不调用$auth = TRUE
或FALSE
,只需像这样返回true或false:
public function runAuth()
{
// do stuff
return true; // or false depending on success.
}
希望能有所帮助。否则,您将需要以某种方式引用MY_Controller对象。例如:
$this->auth_model->runAuth($this);
现在使用您的方法:
public function runAuth(MY_Controller $myctrl)
{
// do stuff
$myctrl->auth = true; // or false
}
另一种选择是使用静态字段:
class MY_Controller extends Controller
{
public static $auth = false;
// the other stuff
}
现在你可以在没有对象引用的情况下更新它:
public function runAuth()
{
// do stuff
MY_Controller::$auth = true;
}
在你的模型中,你可以这样访问它:
if (static::$auth) echo "Boo Yeah!";