I have a web service (and am developing another) which can return various types of data: html, xml, and json. Presently, the only way I know to make this work is to setup a template for each content type and change my calls as follows:
www.tld.com/service.html?action=get_data_html
www.tld.com/service.xml?action=get_data_xml
www.tld.com/service.json?action=get_data_json
I have three resources which all have the same [[service]] call in the content but with different aliases and templates that set the content type.
This seems like a kludge to me and I seek a cleaner way to do it. Is there any way to set the content type from within a snippet to make this interface cleaner?
-
☆ A M B ☆
- 3,141 Posts
You could setup the seperate resources with the right content type (that sets the header for you), or, you could take a plugin approach similar to how InstantAPI does it:
https://github.com/aladage/InstantAPI/blob/master/core/components/instantapi/elements/plugins/InstantAPI.php
There might be a way to use a plugin to change the content type on the fly, maybe OnWebPageLoad or something?
Yes, the three thread approach is exactly what I am doing already as stated above. This is a less than elegant solution however which I would like to avoid using in the future.
Sorry, I said "thread." I meant "resource."
-
☆ A M B ☆
- 24,524 Posts
You should be able to do it with a plugin; the resource type and content type are fields in the site_content table like any other, so they could be changed on-the-fly early in the parsing process by a plugin.
-
☆ A M B ☆
- 24,524 Posts
Hm. That's hard for me to say at this point. I knew how the events were triggered in Evo's parser, but I'm not yet familiar with Revo's parsing process. I would think as soon as the resource is sufficiently loaded for a full resource object to be available but before any processing has begun. The plugin would need to modify the resource object's content_type property. Perhaps OnLoadWebDocument:
Fires directly before the Response is sent and after a Resource is loaded.
The resource can be referenced via $modx->resource.