Skip to content

Commit b83f16c

Browse files
authored
Merge pull request #2908 from jreklund/controllers-and-routing
Improved subjects in Controller and Routing chapter
2 parents a4b86a5 + f8c7796 commit b83f16c

9 files changed

Lines changed: 640 additions & 636 deletions

File tree

user_guide_src/source/incoming/content_negotiation.rst

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ Loading the Class
1515

1616
You can load an instance of the class manually through the Service class::
1717

18-
$negotiator = \Config\Services::negotiator();
18+
$negotiate = \Config\Services::negotiator();
1919

2020
This will grab the current request instance and automatically inject it into the Negotiator class.
2121

2222
This class does not need to be loaded on it's own. Instead, it can be accessed through this request's ``IncomingRequest``
2323
instance. While you cannot access it directly this way, you can easily access all of methods through the ``negotiate()``
2424
method::
2525

26-
$request->negotiate('media', ['foo', 'bar']);
26+
$request->negotiate('media', ['foo', 'bar']);
2727

2828
When accessed this way, the first parameter is the type of content you're trying to find a match for, while the
2929
second 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
4343
wants the data in. This is especially common in API's. For example, a client might request JSON formatted data
4444
from 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

4949
The server now needs to provide a list of what type of content it can provide. In this example, the API might
5050
be 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

6262
In this case, both the client and the server can agree on formatting the data as JSON so 'json' is returned from
6363
the negotiate method. By default, if no match is found, the first element in the $supported array would be returned.
6464
In some cases, though, you might need to enforce the format to be a strict match. If you pass ``true`` as the
6565
final 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

7171
Language
7272
========
@@ -76,20 +76,20 @@ language site, this obviously isn't going to make much difference, but any site
7676
of content will find this useful, since the browser will typically send the preferred language along in the ``Accept-Language``
7777
header::
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

8282
In this example, the browser would prefer French, with a second choice of English. If your website supports English
8383
and 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

9494
In this example, 'en' would be returned as the current language. If no match is found, it will return the first element
9595
in the $supported array, so that should always be the preferred language.
@@ -100,14 +100,14 @@ Encoding
100100
The ``Accept-Encoding`` header contains the character sets the client prefers to receive, and is used to
101101
specify 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

106106
Your 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

112112
See more at `Wikipedia <https://en.wikipedia.org/wiki/HTTP_compression>`_.
113113

@@ -116,12 +116,11 @@ Character Set
116116

117117
The 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

122122
By 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

Comments
 (0)