0

我有一个简单的代码,我把它放在我的控制器的构造函数中

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

我使用此代码来确保注销的安全。我的问题是,有没有办法将我的代码放置/声明为每个控制器的全局代码?因为很难在每个控制器的构造函数上硬编码所有内容。

感谢您的帮助。

4

4 回答 4

2

创建一个核心控制器可能很好,但它将适用于您应用程序中的每个控制器问题是,如果您有一个不想应用该设置的公共页面怎么办。

我建议在您的 Controllers 文件夹中创建一个控制器,并以您喜欢的方式创建它。

例子:

父管理员控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin 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");
    }
}

从管理员继承的控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    include APPPATH.'controllers/admin.php';
    class Dashboard extends Admin {

    public function __construct() {
        parent::__construct();
    }
}

观察 include APPPATH.'controllers/admin.php';并且class Dashboard extends Admin {您需要包含管理控制器以便您可以扩展它。

于 2015-05-15T05:14:09.767 回答
1

您可以使用 CI 钩子,并使用post_controller_constructor钩点调用钩子方法并在钩子中添加标题。

用户指南上提供的集成详细信息单击此处

于 2015-05-15T05:20:57.987 回答
1

您可以通过核心目录扩展默认 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

于 2015-05-15T05:28:13.730 回答
1

我喜欢所有的答案,但最好的方法是使用钩子。首先将此添加到您的 hooks.php

 $hook['display_override'] = array(
    'class'    => 'RouteProcess',
    'function' => 'afterroute',
    'filename' => 'RouteProcess.php',
    'filepath' => 'hooks',
    'params'   => array()
 );

然后进入 hooks/ 文件夹并创建 RoutesProcess.php 创建以下文件:

class RouteProcess{
function afterroute(){
    $this->CI =&get_instance();
     $this->CI->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
    $this->CI->output->set_header("Pragma: no-cache");
    echo $this->CI->output->get_output();
}
}

这样做的好处是它不需要调用可以覆盖的控制器的 __construct()。无论如何都会调用它。

于 2015-05-15T10:18:38.657 回答