PHP Class yii\filters\ContentNegotiator

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 Accept 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 Accept-Language 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. php in application configuration use yii\web\Response; return [ 'bootstrap' => [ [ 'class' => 'yii\filters\ContentNegotiator', 'formats' => [ 'application/json' => Response::FORMAT_JSON, 'application/xml' => Response::FORMAT_XML, ], 'languages' => [ 'en', 'de', ], ], ], ]; 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 only or except property of the filter. php use yii\web\Response; public function behaviors() { return [ [ 'class' => 'yii\filters\ContentNegotiator', 'only' => ['view', 'index'], // in a controller if in a module, use the following IDs for user actions 'only' => ['user/view', 'user/index'] 'formats' => [ 'application/json' => Response::FORMAT_JSON, ], 'languages' => [ 'en', 'de', ], ], ]; }
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\ActionFilter, implements yii\base\BootstrapInterface
Datei anzeigen Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$formatParam the name of the GET parameter that specifies the response format. Note that if the specified format does not exist in [[formats]], a [[UnsupportedMediaTypeHttpException]] exception will be thrown. If the parameter value is empty or if this property is null, the response format will be determined based on the Accept HTTP header only.
$formats list of supported response formats. The keys are MIME types (e.g. application/json) while the values are the corresponding formats (e.g. html, json) which must be supported as declared in [[\yii\web\Response::formatters]]. If this property is empty or not set, response format negotiation will be skipped.
$languageParam the name of the GET parameter that specifies the [[\yii\base\Application::language|application language]]. Note that if the specified language does not match any of [[languages]], the first language in [[languages]] will be used. If the parameter value is empty or if this property is null, the application language will be determined based on the Accept-Language HTTP header only.
$languages 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.
$request the current request. If not set, the request application component will be used.
$response the response to be sent. If not set, the response application component will be used.

Public Methods

Method Description
beforeAction ( $action )
bootstrap ( $app )
negotiate ( ) Negotiates the response format and application language.

Protected Methods

Method Description
isLanguageSupported ( string $requested, string $supported ) : boolean Returns a value indicating whether the requested language matches the supported language.
negotiateContentType ( Request $request, Response $response ) Negotiates the response format.
negotiateLanguage ( Request $request ) : string Negotiates the application language.

Method Details

beforeAction() public method

public beforeAction ( $action )

bootstrap() public method

public bootstrap ( $app )

isLanguageSupported() protected method

Returns a value indicating whether the requested language matches the supported language.
protected isLanguageSupported ( string $requested, string $supported ) : boolean
$requested string the requested language code
$supported string the supported language code
return boolean whether the requested language is supported

negotiate() public method

Negotiates the response format and application language.
public negotiate ( )

negotiateContentType() protected method

Negotiates the response format.
protected negotiateContentType ( Request $request, Response $response )
$request yii\web\Request
$response yii\web\Response

negotiateLanguage() protected method

Negotiates the application language.
protected negotiateLanguage ( Request $request ) : string
$request yii\web\Request
return string the chosen language

Property Details

$formatParam public_oe property

the name of the GET parameter that specifies the response format. Note that if the specified format does not exist in [[formats]], a [[UnsupportedMediaTypeHttpException]] exception will be thrown. If the parameter value is empty or if this property is null, the response format will be determined based on the Accept HTTP header only.
See also: formats
public $formatParam

$formats public_oe property

list of supported response formats. The keys are MIME types (e.g. application/json) while the values are the corresponding formats (e.g. html, json) which must be supported as declared in [[\yii\web\Response::formatters]]. If this property is empty or not set, response format negotiation will be skipped.
public $formats

$languageParam public_oe property

the name of the GET parameter that specifies the [[\yii\base\Application::language|application language]]. Note that if the specified language does not match any of [[languages]], the first language in [[languages]] will be used. If the parameter value is empty or if this property is null, the application language will be determined based on the Accept-Language HTTP header only.
See also: languages
public $languageParam

$languages public_oe 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.
public $languages

$request public_oe property

the current request. If not set, the request application component will be used.
public $request

$response public_oe property

the response to be sent. If not set, the response application component will be used.
public $response