How to use middleware in CodeIgniter to handle requests and responses
Introduction:
CodeIgniter is a lightweight PHP framework that is widely used to develop web applications. As projects grow and become more complex, we often need to pre-process or post-process requests and responses. In order to achieve this purpose, we can use middleware for unified request filtering and response processing. This article will introduce how to use middleware in CodeIgniter.
// application/controllers/Welcome.php
class Welcome extends CI_Controller {
public function index() { $this->load->view('welcome_message'); }
}
// application/views /welcome_message.php
$config['enable_hooks'] = TRUE;
Next, we need to create a middleware class. We create a new Middleware.php file in the application/hooks directory. The code example is as follows:
// application/hooks/Middleware.php
class Middleware {
protected $CI; public function __construct() { $this->CI = &get_instance(); } public function process_request() { // 在这里进行请求的预处理操作 // 例如权限验证、日志记录等 } public function process_response() { // 在这里进行响应的后处理操作 // 例如数据处理、日志记录等 }
}
In the middleware class, we first need to obtain the instance of CodeIgniter (through the get_instance() method), and then save it to a class member variable in the constructor. This way we can use all the features of CodeIgniter in the middleware.
In the process_request() method, we can write code for request preprocessing. For example, we can perform permission verification here. If the user does not have permission to access a certain page or interface, we can abort the request or jump to other pages.
In the process_response() method, we can write code for post-response processing. For example, we can process the response data here or record logs.
$hook['post_controller_constructor'] = array(
'class' => 'Middleware', 'function' => 'process_request', 'filename' => 'Middleware.php', 'filepath' => 'hooks'
);
$hook ['post_controller'] = array(
'class' => 'Middleware', 'function' => 'process_response', 'filename' => 'Middleware.php', 'filepath' => 'hooks'
);
In the above code, we use two hooks: post_controller_constructor and post_controller.
The post_controller_constructor hook is called after the controller constructor, but before calling the controller method. This is a good time to do some preprocessing of the request.
The post_controller hook is called after calling the controller method, but before sending the response to the client. This is a good time to do some post-processing of the response.
// application/controllers/Welcome.php
class Welcome extends CI_Controller {
public function index() { // 打印出请求信息,测试中间件工作正常 print_r($this->input->server('REQUEST_URI')); exit; }
}
This code will print out the requested URI before the controller method is executed and stop program execution. This way we can check if the middleware handled the request correctly.
The above is the detailed content of How to use middleware in CodeIgniter to handle requests and responses. For more information, please follow other related articles on the PHP Chinese website!