PHP Класс 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', ], ], ]; }
С версии: 2.0
Автор: Qiang Xue ([email protected])
Наследование: extends yii\base\ActionFilter, implements yii\base\BootstrapInterface
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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.

Открытые методы

Метод Описание
beforeAction ( $action )
bootstrap ( $app )
negotiate ( ) Negotiates the response format and application language.

Защищенные методы

Метод Описание
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.

Описание методов

beforeAction() публичный Метод

public beforeAction ( $action )

bootstrap() публичный Метод

public bootstrap ( $app )

isLanguageSupported() защищенный Метод

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
Результат boolean whether the requested language is supported

negotiate() публичный Метод

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

negotiateContentType() защищенный Метод

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

negotiateLanguage() защищенный Метод

Negotiates the application language.
protected negotiateLanguage ( Request $request ) : string
$request yii\web\Request
Результат string the chosen language

Описание свойств

$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
public $formatParam

$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.
public $formats

$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
public $languageParam

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

$request публичное свойство

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

$response публичное свойство

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