We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 813 ☆ A M B ☆
    • 326 Posts
    Here's a fun one ;-)

    I'm working on a plugin that triggers on the OnPageNotFound event, checks if the URL contains "json" or "xml" as a file extension, and then attempts to return the page's content in that format (basically, an ad-hoc API).

    However, the tricky part of that is determining what page the user is looking for in the first place. I'd like to do this by first determining the resource's ID, which would then allow me to do pretty much whatever I want with its content. However, as you'd expect, OnPageNotFound doesn't have access to the resource object (according to http://rtfm.modx.com/display/revolution20/OnPageNotFound). I also have paths turned on, which means I can't just grab the alias; I need to basically traverse the entire URI.

    Has anyone attempted this? I think I can figure it out, but if someone's already done it and has sample code, it would be appreciated. Thanks! [ed. note: aladage last edited this post 14 years, 2 months ago.]
      • 37054
      • 93 Posts
      You can't access the resource but you can access the URL using the $_SERVER array. Would depend on your context usage how this would work but you can maybe grab the context of the request using

      $modx->context->key
      


      and then use that and the alias to find the resource.

      You'd need to give more info on your context setup and the URL's that access them.

      Of course the other option would be to just use the path and alias in the URL your provide for using the API and let users define the format using a query parameter - think that would be my preferred option - with a little addition to your URL rewrites you could still have simple URL's like mypage/json or mypage/xml

        • 813 ☆ A M B ☆
        • 326 Posts
        I don't think pinpointing the context helps that much. It's a big site, and there's a very good chance that (for example) there could be 10 pages in the same context with an alias of "overview" that look practically identical in the site_content table. For example:

        domain.com/context1/path/one/overview.json
        domain.com/context1/path/two/overview.json

        However, I do really like your idea of a query string parameter instead of an extension. Instead of using OnPageNotFound, I just grab the ID on page load and possibly mix in $modx->sendForward to grab the content from a JSON-generating utility page.

        Now, the tricky part: which event has access to the resource object, but before the page loads? I don't think OnWebPagePrerender will do it: http://rtfm.modx.com/display/revolution20/OnWebPagePrerender
          • 813 ☆ A M B ☆
          • 326 Posts
          Mark Hamstra strikes again! From Twitter:

          $page = $modx->getObject('modResource',array('uri' => str_replace('.json','.html',$_SERVER['REQUEST_URI']));


          So simple. I didn't realize URI is stored with the resource in the site_content table. That completely solves my problem!
            • 37054
            • 93 Posts
            OnWebDocumentLoad
              • 813 ☆ A M B ☆
              • 326 Posts
              Good call, thanks. Although I guess I don't need it now.