Skip to content
PHP Ecosystem
Explore

ContentNegotiator

API reference › yii › filters › ContentNegotiator

ContentNegotiator supports response format negotiation and application language negotiation.

When the 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 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 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.

// 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.

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',
],
],
];
}
PropertyTypeDescriptionDefined by
$exceptarrayList of action IDs that this filter should not apply to. Default: [].yii\base\ActionFilter
$formatParamstringThe name of the GET parameter that specifies the response format. Default: '_format'.ContentNegotiator
$formatsarray|nullList of supported response formats. Default: null.ContentNegotiator
$languageParamstringThe name of the GET parameter that specifies the application language. Default: '_lang'.ContentNegotiator
$languagesarray|nullA list of supported languages. Default: null.ContentNegotiator
$onlyarrayList of action IDs that this filter should apply to. Default: [].yii\base\ActionFilter
$owneryii\base\T|nullThe owner of this behavior Default: null.yii\base\Behavior
$requestyii\web\RequestThe current request. Default: null.ContentNegotiator
$responseyii\web\Response|nullThe response to be sent. Default: null.ContentNegotiator
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
afterAction()This method is invoked right after an action is executed.yii\base\ActionFilter
afterFilter()yii\base\ActionFilter
attach()Attaches the behavior object to the component.yii\base\ActionFilter
beforeAction()This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.ContentNegotiator
beforeFilter()yii\base\ActionFilter
bootstrap()Bootstrap method to be called during application bootstrap stage.ContentNegotiator
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detach()Detaches the behavior object from the component.yii\base\ActionFilter
events()Declares event handlers for the $owner’s events.yii\base\Behavior
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
negotiate()Negotiates the response format and application language.ContentNegotiator
getActionId() protectedReturns an action ID by converting Action::$uniqueId into an ID relative to the module.yii\base\ActionFilter
isActive() protectedReturns a value indicating whether the filter is active for the given action.yii\base\ActionFilter
isLanguageSupported() protectedReturns a value indicating whether the requested language matches the supported language.ContentNegotiator
negotiateContentType() protectedNegotiates the response format.ContentNegotiator
negotiateLanguage() protectedNegotiates the application language.ContentNegotiator
public string $formatParam = '_format'

The name of the GET parameter that specifies the response format.

Note that if the specified format does not exist in $formats, a NotAcceptableHttpException 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.

public array|null $formats = null

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 Response::$formatters.

If this property is empty or not set, response format negotiation will be skipped.

public string $languageParam = '_lang'

The name of the GET parameter that specifies the 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.

public array|null $languages = null

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 yii\web\Request $request = null

The current request.

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

public yii\web\Response|null $response = null

The response to be sent.

If not set, the response application component will be used.

public function beforeAction(mixed $action): bool

This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.

Parameters

ParameterTypeDescription
$actionmixedThe action to be executed.

Returns

bool: Whether the action should continue to be executed.

public function bootstrap(mixed $app): mixed

Bootstrap method to be called during application bootstrap stage.

Parameters

ParameterTypeDescription
$appmixedThe application currently running

Returns

mixed

public function negotiate(): mixed

Negotiates the response format and application language.

Returns

mixed

protected function isLanguageSupported(string $requested, string $supported): bool

Returns a value indicating whether the requested language matches the supported language.

Parameters

ParameterTypeDescription
$requestedstringThe requested language code
$supportedstringThe supported language code

Returns

bool: Whether the requested language is supported

protected function negotiateContentType(yii\web\Request $request, yii\web\Response $response): mixed

Negotiates the response format.

Parameters

ParameterTypeDescription
$requestyii\web\Request
$responseyii\web\Response

Returns

mixed

Throws

protected function negotiateLanguage(yii\web\Request $request): string

Negotiates the application language.

Parameters

ParameterTypeDescription
$requestyii\web\Request

Returns

string: The chosen language