Home >Backend Development >PHP Tutorial >How to use middleware in CodeIgniter to handle requests and responses
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
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
b2386ffb911b14667cb8f0f91ea547a7Welcome to CodeIgniter6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
4a249f0d628e2318394fd9b75b4636b1Welcome to CodeIgniter473f0a7621bec819994bb5020d29372a
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
$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!