@@ -15,15 +15,15 @@ Loading the Class
1515
1616You can load an instance of the class manually through the Service class::
1717
18- $negotiator = \Config\Services::negotiator();
18+ $negotiate = \Config\Services::negotiator();
1919
2020This will grab the current request instance and automatically inject it into the Negotiator class.
2121
2222This class does not need to be loaded on it's own. Instead, it can be accessed through this request's ``IncomingRequest ``
2323instance. While you cannot access it directly this way, you can easily access all of methods through the ``negotiate() ``
2424method::
2525
26- $request->negotiate('media', ['foo', 'bar']);
26+ $request->negotiate('media', ['foo', 'bar']);
2727
2828When accessed this way, the first parameter is the type of content you're trying to find a match for, while the
2929second is an array of supported values.
@@ -43,30 +43,30 @@ is one of the most complex headers available. A common example is the client tel
4343wants the data in. This is especially common in API's. For example, a client might request JSON formatted data
4444from an API endpoint::
4545
46- GET /foo HTTP/1.1
47- Accept: application/json
46+ GET /foo HTTP/1.1
47+ Accept: application/json
4848
4949The server now needs to provide a list of what type of content it can provide. In this example, the API might
5050be able to return data as raw HTML, JSON, or XML. This list should be provided in order of preference::
5151
52- $supported = [
53- 'application/json',
54- 'text/html',
55- 'application/xml'
56- ];
52+ $supported = [
53+ 'application/json',
54+ 'text/html',
55+ 'application/xml'
56+ ];
5757
58- $format = $request->negotiate('media', $supported);
59- // or
60- $format = $negotiate->media($supported);
58+ $format = $request->negotiate('media', $supported);
59+ // or
60+ $format = $negotiate->media($supported);
6161
6262In this case, both the client and the server can agree on formatting the data as JSON so 'json' is returned from
6363the negotiate method. By default, if no match is found, the first element in the $supported array would be returned.
6464In some cases, though, you might need to enforce the format to be a strict match. If you pass ``true `` as the
6565final value, it will return an empty string if no match is found::
6666
67- $format = $request->negotiate('media', $supported, true);
68- // or
69- $format = $negotiate->media($supported, true);
67+ $format = $request->negotiate('media', $supported, true);
68+ // or
69+ $format = $negotiate->media($supported, true);
7070
7171Language
7272========
@@ -76,20 +76,20 @@ language site, this obviously isn't going to make much difference, but any site
7676of content will find this useful, since the browser will typically send the preferred language along in the ``Accept-Language ``
7777header::
7878
79- GET /foo HTTP/1.1
80- Accept-Language: fr; q=1.0, en; q=0.5
79+ GET /foo HTTP/1.1
80+ Accept-Language: fr; q=1.0, en; q=0.5
8181
8282In this example, the browser would prefer French, with a second choice of English. If your website supports English
8383and German you would do something like::
8484
85- $supported = [
86- 'en',
87- 'de'
88- ];
85+ $supported = [
86+ 'en',
87+ 'de'
88+ ];
8989
90- $lang = $request->negotiate('language', $supported);
91- // or
92- $lang = $negotiate->language($supported);
90+ $lang = $request->negotiate('language', $supported);
91+ // or
92+ $lang = $negotiate->language($supported);
9393
9494In this example, 'en' would be returned as the current language. If no match is found, it will return the first element
9595in the $supported array, so that should always be the preferred language.
@@ -100,14 +100,14 @@ Encoding
100100The ``Accept-Encoding `` header contains the character sets the client prefers to receive, and is used to
101101specify the type of compression the client supports::
102102
103- GET /foo HTTP/1.1
104- Accept-Encoding: compress, gzip
103+ GET /foo HTTP/1.1
104+ Accept-Encoding: compress, gzip
105105
106106Your web server will define what types of compression you can use. Some, like Apache, only support **gzip **::
107107
108- $type = $request->negotiate('encoding', ['gzip']);
109- // or
110- $type = $negotiate->encoding(['gzip']);
108+ $type = $request->negotiate('encoding', ['gzip']);
109+ // or
110+ $type = $negotiate->encoding(['gzip']);
111111
112112See more at `Wikipedia <https://en.wikipedia.org/wiki/HTTP_compression >`_.
113113
@@ -116,12 +116,11 @@ Character Set
116116
117117The desired character set is passed through the ``Accept-Charset `` header::
118118
119- GET /foo HTTP/1.1
120- Accept-Charset: utf-16, utf-8
119+ GET /foo HTTP/1.1
120+ Accept-Charset: utf-16, utf-8
121121
122122By default, if no matches are found, **utf-8 ** will be returned::
123123
124- $charset = $request->negotiate('charset', ['utf-8']);
125- // or
126- $charset = $negotiate->charset(['utf-8']);
127-
124+ $charset = $request->negotiate('charset', ['utf-8']);
125+ // or
126+ $charset = $negotiate->charset(['utf-8']);
0 commit comments