What's the best way to response with different format when requesting the same URL with diffrent Content-Type

I am using laminas-mvc, when requesting /appliation/index/demo with Content-Type: application/json, then I response with json format, when xml then response xml format, if not specially set the content-type, when It will response a html format.

I am wondering how to fulfil this condition. Or where is the best place to hook the logic.
I don’t know if anyone have done this before. any advises?

Please check the controller plugin “AcceptableViewModelSelector”.

thank you! it seems exactly what I am looking for!!