Data-id= "1190000004868343" >
When I focus on the HTTP layer, I find a lot of CIer are not very familiar with content negotiation, let's explore what is content negotiation and how to use him in the upcoming CodeIgniter 4.
What is content negotiation?
In short, content negotiation refers to the client and server side to negotiate the content of the response, and then provide the most appropriate resources for the client. Content negotiation is a benchmark that responds to the language of the resource, the type of picture, and the encoding method (some of the fields included in the request header Accept
are the benchmarks for the judgment).
For example, when I visit Mozilla's site with Chrome, I can see the following HTTP request header information:
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Accept-encoding:gzip, deflate, SDCH
accept-language:en-us,en;q=0.8
This accept
information tells us the format supported by the browser and provides priority information for these formats (priority is determined by the value of Q). The above information indicates that the browser would prefer to receive text/html types of content in all supported content types. Because my browser's language setting is English, the accept-language
request header indicates that I prefer the American English (en-us) page.
Obviously, even if we do not provide any content negotiation information, the WEB site can still run as usual, and we have been doing this for many years. However, the fact that a WEB server can help us deal with some form of content negotiation is not always very good, but it does not mean that the server cannot handle this information.
There are two fascinating uses for content negotiation, one for sites that support multiple languages, and another for API interfaces that return specific format data.
Do you have to use content negotiation? Perhaps not necessarily, he may be a double-edged sword, some people suggest not to use him, and some people think he is like sliced bread as a favorite. But if you want, it's easy to use content negotiation in CodeIgniter.
A simple example
I'm not going to go into too much detail about the content negotiation here (the details will be written in the user's manual), which is a simple example of how content negotiation determines the output language.
class BaseController extends \CodeIgniter\Controller{ protected $language; public function __construct(...$params) { parent::__construct(...$params); $supportedLangs = ['en-US', 'en', 'fr']; $this->language = $this->request->negotiate('language', $supportedLangs); }}
This example indicates that the site can support both English and French, we will assign the supported language to the $supportedLangs
array, indicating that the default language is American English, but also support General English and French, and then simply call the $negotiate->language()
method, pass the supported language type, parsing can recognize the correct HTTP header, The best matching results are then returned according to the order of precedence defined in the array. If both languages do not match, the first language in the array is used.
The 4 negotiation methods in the Negotiate class are:
Media () differs from the usual Accept
request header, which he can use to request different versions of Html/text, or audio support, image support, and so on.
CharSet () differs from the Accept-Charset
request header, and if there is no match, the default value is UTF-8.
Encoding () differs from the Accept-Encoding
request header and can determine which compression type is used by any client.
Language () differs from the Accept-Language
request header.
Not all scenarios need content negotiation, but he is a powerful tool for building high-quality APIs and can be used creatively elsewhere.
The above describes the CodeIgniter 4 content negotiation, including the content of the CodeIgniter, I hope that the PHP tutorial interested in a friend helpful.