data-id="1190000004868343">
When I paid attention to the HTTP layer, I found that many CIers didn't know much about content negotiation. Let's discuss what content negotiation is and how to use it in the upcoming CodeIgniter 4. he.
What is content negotiation?
In short, content negotiation means that the client and the server negotiate the response resource content, and then provide the client with the most suitable resource. Content negotiation will use the language, image type and encoding method of the response resource as the basis for judgment (certain Accept
fields included in the request header are the basis for judgment).
For example, when I use Chrome to access Mozilla’s site, 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
These accept
information tell our browser Supported formats and provide priority information for these formats (the priority is determined by the value of q). The above information indicates that the browser prefers to receive text/html type content among all supported content types. Since the language setting of my browser is English, the accept-language
request header indicates that I prefer American English (en-US) pages.
Obviously, the website can still function even if we don't provide any content negotiation information, and we have done so for many years. However, the fact that web servers can handle some forms of content negotiation for us, which we are generally not very good at taking advantage of, does not mean that the server cannot handle this information.
Content negotiation has two very attractive uses, one is for sites that support multiple languages, and the other is an API interface for returning data in a specific format.
Is it necessary to use content negotiation? Maybe not necessarily, he may be a double-edged sword, some people advise against using him, others think he is as lovable as sliced bread. But if you want to use it, it's easy to use content negotiation in CodeIgniter.
A simple example
I will not introduce content negotiation in too much detail here (detailed introduction will be written in the user manual). This example briefly introduces how content negotiation determines the output language.
<code>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); } }</code>
This example indicates that the site can support English and French. We assign the supported languages to the $supportedLangs
array, which indicates that the default language is American English, but ordinary English and French are also supported, and then simply call $negotiate ->language()
method, passing the supported language type, can identify the correct HTTP header during parsing, and then return the most matching result according to the priority order defined in the array. If neither language matches, the first language in the array is used. The four negotiation methods in the
Negotiate class are:
media(). Different from the usual Accept
request header, it can be used to request different versions of html/text, or audio support, image support, etc. wait.
charset() is different from the Accept-Charset
request header and defaults to UTF-8 if no match is found.
encoding() is different from the Accept-Encoding
request header and can determine the compression type supported by any client.
language() is different from the Accept-Language
request header.
Not all scenarios require content negotiation, but it is a powerful tool for building high-quality APIs, and can also be used creatively in other places.
The above introduces the content negotiation of CodeIgniter 4, including the content of codeigniter. I hope it will be helpful to friends who are interested in PHP tutorials.