您可以通过核心目录扩展默认 CI_Controller
在 application/core/MY_Controller.php 中:(MY_ 部分在您的 config.php 中定义)
class BaseController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
}
}
然后在您的控制器中使用:
class ControllerXYZ extends BaseController {
//your code
}
如果您的控制器不需要 BaseController 的功能,请不要从 basecontroller 扩展,而是从 CI_Controller 扩展:
class ControllerXYZ extends CI_Controller {
//your code without the headers set
}
这还具有删除每个控制器需要的更多代码重复数据的优点,例如检查是否有人登录,您可以这样:
class BaseController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
if(!$this->session->userdata('loggedIn') === True) {
redirect('/loginpage');
}
}
}
有关更多信息,请参阅https://ellislab.com/codeigniter/user-guide/general/core_classes.html。