We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15510
    • 70 Posts
    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?
      • 40092
      • 265 Posts
        • 15510
        • 70 Posts
        Quote from: thapriest86 at Dec 06, 2012, 04:10 PM
        You could use this: http://php.net/manual/en/function.header.php

        I looked at that as an option. I'm betting it would collide with the header information that MODx automatically generates from the value specified in the template. Am I wrong?
          • 18373 ☆ 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?
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 40092
            • 265 Posts
            Quote from: kc64 at Dec 06, 2012, 04:17 PM

            I looked at that as an option. I'm betting it would collide with the header information that MODx automatically generates from the value specified in the template. Am I wrong?

            Hmm, you might be right, haven't put this in to test, just came to mind. In that case i'd personally go for setting up separate resources, like Mark mentioned above.
              • 15510
              • 70 Posts
              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.
                • 15510
                • 70 Posts
                Sorry, I said "thread." I meant "resource."
                  • 28042 ☆ 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.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 15510
                    • 70 Posts
                    Quote from: sottwell at Dec 06, 2012, 11:42 PM
                    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.

                    Interesting idea. To what event would you bind the implementation? I looked at OnWebPageComplete, OnWebPageInit, and OnWebPagePrerender but none of these seems appropriate.
                      • 28042 ☆ 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.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org