CodeIgniter 4 Request and Response CodeIgniter thinkphp CodeIgniter 3.0 codeigniter cms

Source: Internet
Author: User
Tags codeigniter
Data-id= "1190000004868315" >

CodeIgniter 4 has made a big change in the way the inputs and outputs are handled. In the previous release, including the latest version of V3, the inputs and outputs were handled using two classes containing related functions. There is no advanced technology behind this approach, but it is simple and straightforward to implement the function. In version V4, we will be more modular in the HTTP layer and build a new class structure to handle HTTP requests and responses at the same time.

Overview

When developing a Web app (unlike a CLI program), you only need to care about two classes: IncomingRequest and Response .

Incomingrequest class

The Incomingrequest class contains the HTTP request and the data that accompanies the request, including:

    • Environment variables such as GET, POST, SERVER, and ENV

    • HTTP Request Header

    • Cookies

    • The URL object for the current request

    • Uploaded files

It also includes common request information such as:

    • IP Address of the client

    • is the Ajax request

    • Whether to request for CLI

    • Is HTTPS

If you are surprised by the name Incomingrequest, or is incomingrequest simply called a 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 does only two things: one is that the browser client sends the request to the server (the connection), or the current server sends the request to the external server (the connection).

Response class

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

    • Set the appropriate No-cache header information

    • Handling HTTP Cache Header information

    • redirect page

A simple example

These seem to be very technical, but they are very simple. Instances of these classes have been placed in each controller as attributes, and if you find it cumbersome, you do not need to use these properties directly. The Response class captures the output of the controller and is automatically set to the body of the response. A simple Hello world looks like this:

class Home extends \CodeIgniter\Controller{    public function index()    {        echo "Hello World!";    }}

Easy.

When needed, the framework gives you the ability to precisely control the response. You can create complex HTTP caching policies and customize the response content with the Incomingrequest class through content negotiation.

Here's a slightly more complicated example, and you'll find that the code is easy to read and easy to handle.

  class Home extends \codeigniter\controller{public function __construct (        ... $params) {parent::__construct (... $params); This controller was only accessible via HTTPS if (! $this->request->issecure ()) {//Redi Rect the user to this page via HTTPS, and set the strict-transport-security//headers so the browser would autom            Atically Convert all links to this page to HTTPS//for the next year.        Force_https ();        }} public Function index () {$data = [...];        Set some HTTP cache rules for the this page. $this->response->setcache ([' max-age ' +, ' s-max-age ' = ', ' ETag ' => ;        ' foo ']); Return JSON $this->response->setcontenttype (' Application/json ')->setoutput (json_    Encode ($data)); }}

In this case, we have done three things mainly. First, by redirecting the current URL to the HTTPS URL and setting up a strict-transport-security response header (which is supported by many mainstream browsers, the HTTP request is automatically converted to an HTTPS request via the browser before the request is sent). To force this page to be accessed HTTPS, and then we set some HTTP caching rules to help the browser handle the cache correctly, which means reducing the amount of HTTP requests, reducing the load on the server and improving performance; Finally, we output the JSON data to the user and ensure that the content type is correct.

Hopefully this article will help you get a rough idea of CodeIgniter's future and make everyone realize that change is not scary. :) The future will finalize the framework in more detail until a relatively stable architecture is formed, and more articles will be written to describe the content.

The above describes the CodeIgniter 4 request and response, including the codeigniter aspects of the content, I hope that the PHP tutorial interested in a friend to help.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.