We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40735
    • 119 Posts
    I want to create some ReSTful services and I'm wondering that the best way to go about doing that in MODx might be. I tried creating a static resource with the JSON content type and pointing it to a PHP file hoping that I could output some JSON from a PHP script but it just spit out my code instead of parsing the PHP.

    I thought about just creating new documents for them, embedding snippets in the content and not setting a template for it. It seems like this would work but I'm not sure it's the most elegant solution to the problem.

    Thoughts?
    Thanks!
    Dave
      • 44793
      • 19 Posts
      Creating a resource and embedding a snippet seems to be one way to do it. The snippet could set the MIME type for the resource so it could output JSON or whatever.

      I also wonder if there's a better way to do it.

      - Mike
        • 3749
        • 24,544 Posts
        You can put a PHP file anywhere and provide a link to it directly. If the file in the URL is found, MODX won't rout things through the index.php file and there won't be any restrictions on having code in the file. Obviously there is the potential for serious security issues, so you'd have to be very careful about sanitizing any input.

        Putting a snippet on a page would probably be safer. I think I would create a processor class file that extends modProcessor (which has nice built-in security and JSON production methods among other things) and then call $modx->runProcessor() in your snippet. Depending on your output, you might be able to extend the modObjectGetListProcessor, which descends from modProcessor and overrides some of its methods.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 44793
          • 19 Posts
          Thanks for your input. I'll have to look into modProcessor.