data-id="1190000004868315">
CodeIgniter 4 has made a major change in the way input and output are processed. In previous versions, including the latest V3 version, input and output were handled using two classes containing related functions. Although there is no advanced technology behind this processing method, it can realize the function simply and directly. In V4, we will make the HTTP layer more modular and build a new class structure to handle both HTTP requests and responses.
Overview
When developing web applications (unlike CLI programs), you only need to care about two classes: IncomingRequest
and Response
.
IncomingRequest class
The IncomingRequest class contains the HTTP request and the data attached to the request, including:
Environment variables such as GET, POST, SERVER and ENV
HTTP request headers
Cookie
The things mentioned above may seem very technical, but they are actually very simple. Instances of these classes have been put into each controller as properties, so you don't need to use these properties directly if that bothers you. The Response class captures the controller's output and automatically sets it as the body of the response. A simple Hello World looks like this:
<code>class Home extends \CodeIgniter\Controller { public function index() { echo "Hello World!"; } }</code>
Easy.
The framework gives you the ability to precisely control the response when needed. You can create complex HTTP caching strategies and work with the IncomingRequest class to customize response content through content negotiation.
The following is a slightly more complex example. You will find that the code is easy to understand and easy to process.
<code>class Home extends \CodeIgniter\Controller { public function __construct(...$params) { parent::__construct(...$params); // This controller is only accessible via HTTPS if (! $this->request->isSecure()) { // Redirect the user to this page via HTTPS, and set the Strict-Transport-Security // header so the browser will automatically convert all links to this page to HTTPS // for the next year. force_https(); } } public function index() { $data = [ ... ]; // Set some HTTP cache rules for this page. $this->response->setCache([ 'max-age' => 300, 's-max-age' => 900, 'etag' => 'foo' ]); // Return JSON $this->response->setContentType('application/json') ->setOutput(json_encode($data)); } }</code>
In this example, we mainly do three things. First, by redirecting the current URL to an HTTPS URL and setting a Strict-Transport-Security response header (this method has been supported by many mainstream browsers, the browser automatically converts the HTTP request into an HTTPS request before sending the request ) to force this page to be accessed via HTTPS; then, we set some HTTP caching rules to help the browser handle caching correctly, which means that it can reduce the number of HTTP requests, reduce the load on the server, and improve performance; finally, we output JSON data to the user and make sure the content type is correct.
I hope this article can help everyone have a rough understanding of the future of CodeIgniter and make everyone realize that change is not scary. :) In the future, more details of the framework will be finalized until a relatively stable architecture is formed, and more articles will be written to talk about these contents.
The above introduces the requests and responses of CodeIgniter 4, including the content of codeigniter. I hope it will be helpful to friends who are interested in PHP tutorials.