将代码应用于 codeigniter 中的所有控制器
apply codes to all controller in codeigniter
我有一个简单的代码,我将它放在控制器的构造函数中
$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");
我使用此代码是为了安全注销。我的问题是,有没有办法 put/declare 我的代码对于我的每个控制器都是全局的?因为很难在每个控制器的构造函数上硬编码所有内容。
感谢您的帮助。
创建一个核心控制器可能很好,但它会应用于您应用程序中的每个控制器问题是,如果您有一个您不想应用该设置的 public 页面怎么办。
我建议在您的 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 {
您需要包含管理控制器以便扩展它。
可以使用CI个hook,使用post_controller_constructor
个hook点调用hook方法,在hook中添加headers。
用户指南中提供了集成详细信息click here
您可以通过核心目录扩展默认值 CI_Controller
in 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。
我喜欢所有答案,但最好的方法是使用挂钩。
首先将此添加到您的 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() 可以被覆盖。
这个无论如何都会被调用。
我有一个简单的代码,我将它放在控制器的构造函数中
$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");
我使用此代码是为了安全注销。我的问题是,有没有办法 put/declare 我的代码对于我的每个控制器都是全局的?因为很难在每个控制器的构造函数上硬编码所有内容。
感谢您的帮助。
创建一个核心控制器可能很好,但它会应用于您应用程序中的每个控制器问题是,如果您有一个您不想应用该设置的 public 页面怎么办。
我建议在您的 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 {
您需要包含管理控制器以便扩展它。
可以使用CI个hook,使用post_controller_constructor
个hook点调用hook方法,在hook中添加headers。
用户指南中提供了集成详细信息click here
您可以通过核心目录扩展默认值 CI_Controller
in 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。
我喜欢所有答案,但最好的方法是使用挂钩。 首先将此添加到您的 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() 可以被覆盖。 这个无论如何都会被调用。