We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40041
    • 36 Posts
    Hey all,

    Thanks in advance for reviewing this. I'm a little baffled. I'm working on an AJAX based update form and mechanically, everything seems to be working well.

    A user browses the front end, edits the contents of that particular resource using a form, and the contents get pushed via AJAX to a PHP with the following:

    define('MODX_API_MODE', true);
    require_once('/home/website/public_html/index.php');
    $modx = new modX();
    $modx->initialize('mgr');
    $doc = $modx->getObject('modResource',$_POST["resource_id"]);
    $doc->setContent($_POST["content"]);
    $doc->save();
    


    The form is posting - the contents are being saved in the resource. HOWEVER smiley the front end pages are not reflecting the changes. I have confirmed on the back end that the resources have indeed been updated. I can force the changes to be visible on the front end only by either clearing the cache, or saving the resource on the back-end - effectively clearing the cache for that resource I believe.

    I have tried a number of code-based cache clearing examples I have found while searching for a solution, however none appear to do anything - including log an error indicating that they are doing anything.

    I have also tried saving/updating via the formit / resource update hook snippet examples I have found. It also works, however the same behavior as outlined above occurs.

    Anyone have a foolproof method of clearing the cache from a php/snippet/api?

    Any ideas? Thanks!

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      Have you tried NewsPublisher (which will do all that for you and handles the security issues)?

      If you do need to roll your own, you can try something like this to clear the cache for that resource (assuming that you're using the default caching system):

      @unlink(MODX_CORE_PATH  . 'cache/resource/web/resources/' . $resource->get('id') . '.cache.php');


      Be sure to sanitize the $_POST data, otherwise you have a massive security problem. If your page might contain MODX tags, you also have to deal with them somehow or they create another security issue.
        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
        • 40041
        • 36 Posts
        Thanks Bob! We can always count on you! Your timing on this one though was literally about 60 seconds after I found a working solution:

        $modx->cacheManager->refresh();
        


        This seemed to have done the trick - I will however try your solution as it's more targeted.

        I will take a deeper look at NewsPublisher as well.

        Thanks