CodeIgniter 4 requests and responses codeigniter thinkphp codeigniter 3.0 codeigniter cms

WBOY
Release: 2016-07-29 08:54:41
Original
1026 people have browsed it

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

  • R The current requested URL object

  • files uploaded by the file

and also include common request information, such as:

  • The IP address of the client

  • Is it the Ajax request?

  • Is it HTTPS

  • If you are curious about the class name IncomingRequest, or can IncomingRequest be simply called Request? The answer is no, because there is already another more general Request class that contains variables such as GET and POST, but this class does not include detailed HTTP request information. A request usually only does two things: one is that the browser client sends a request to the server (incoming connection), or the current server sends a request to an external server (outgoing connection).

    Response class
Response class is used to return the execution results of the program to the client. You can set HTTP response headers, or send content directly to the client, etc. The Response class provides some convenient methods such as:

Set the appropriate no-cache header information

  • Handle HTTP cache header information

  • Redirect the page

  • A simple example

    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>
    Copy after login

    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>
    Copy after login

    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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!