When the [[formats|supported formats]] property is specified, ContentNegotiator will support response format
negotiation based on the value of the GET parameter [[formatParam]] and the
HTTP header.
If a match is found, the [[Response::format]] property will be set as the chosen format.
The [[Response::acceptMimeType]] as well as [[Response::acceptParams]] will also be updated accordingly.
When the [[languages|supported languages]] is specified, ContentNegotiator will support application
language negotiation based on the value of the GET parameter [[languageParam]] and the
HTTP header.
If a match is found, the [[\yii\base\Application::language]] property will be set as the chosen language.
You may use ContentNegotiator as a bootstrapping component as well as an action filter.
The following code shows how you can use ContentNegotiator as a bootstrapping component. Note that in this case,
the content negotiation applies to the whole application.
The following code shows how you can use ContentNegotiator as an action filter in either a controller or a module.
In this case, the content negotiation result only applies to the corresponding controller or module, or even
specific actions if you configure the
negotiateContentType()
protected method
Negotiates the response format.
protected negotiateContentType ( Request $request, Response $response ) |
$request |
yii\web\Request |
|
$response |
yii\web\Response |
|
$languages public property
a list of supported languages. The array keys are the supported language variants (e.g. en-GB, en-US),
while the array values are the corresponding language codes (e.g. en, de) recognized by the application.
Array keys are not always required. When an array value does not have a key, the matching of the requested language
will be based on a language fallback mechanism. For example, a value of en will match en, en_US, en-US, en-GB, etc.
If this property is empty or not set, language negotiation will be skipped.