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
파일 보기 프로젝트 열기: yiisoft/yii2 1 사용 예제들

공개 프로퍼티들

프로퍼티 타입 설명
$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