Answer the question
In order to leave comments, you need to log in
Why is the ContentNegotiator filter needed in Yii2?
The purpose of this filter is not entirely clear.
The documentation says:
ContentNegotiator supports negotiation of response format and application language. It tries to determine the response format and/or language by checking the GET parameters and the Accept HTTP header.
Answer the question
In order to leave comments, you need to log in
Here is the documentation https://github.com/yiisoft/yii2/blob/master/docs/g...
The main purpose is to return the response in the correct format.
In the Accept header, the client indicates in what format he needs the data, for example, json or xml.
From the action, you can return not only a string, but a model, an array (including models), a data provider, etc.
The ContentNegotiator converts this data to the required format - for example, it serializes the model to json, sets the headers, etc. Serialization rules can be described in the model, or you can implement your own serializer (in the documentation, alas, this is scattered in different places). In general, to create an api - a necessary thing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question