We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45892
    • 9 Posts
    I am seeing inconsistent behavior from modx with my snippets. To debug my snippets I started creating regression testing scripts that run my snippets using the API. Through this I've discovered that the login snippet is misbehaving as well. Here is a sample of code I'm using:

    define('MODX_CORE_PATH', '/path/to/core/');
    define('MODX_CONFIG_KEY', 'config');
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    
    $modx = new modX();
    $modx -> setDebug(TRUE);
    $modx -> initialize('web');
    $modx->getService('error','error.modError');
    
    $response = $modx->runProcessor('security/login', array( 'username'=>'someuser','password'=>'pw'));
    
    $output = $modx->runSnippet('mysnippet', array('key'=>'value'));
    


    Pretty standard, right? This code works, but not always. When the code is unsuccessful, $modx->user->get('username') shows '(anonymous)' as the user (id=0); my snippets just don't work. If I log the user in or out on the website, my api script works fine for a while but then stops working (until I log in or out again).

    I'm not seeing much of a pattern in what I'm doing that would trigger the failure. I can run the script 3 times without changing anything and it will suddenly not work on the 4th time. I rarely get it to work past about 5 runs, it always works at least once after logging in or out.

    Very odd behavior. Any ideas why this might be happening?

    Edit: I should point out that my snippets occasionally stop working on the website. That was the reason for building the regression test scripts.

    Edit: Logging in on the website side of things seems to be working all the time. My snippets, however, don't work on website at times. My thought process is that if I can pin down what is happening in my testing scripts using the modx api, I will be able to address the issues on the website.

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

    [ed. note: shanderson last edited this post 10 years, 3 months ago.]
      • 45892
      • 9 Posts
      Still no luck tracking down what is triggering the issue. I needed a solution so I rigged a work around.

      The idea for my solution came from the Wayfinder source code. I wondered why they used the following code to load chunks instead of modx->getChunk:
              if ($chunk= $this->modx->getObject('modChunk', array ('name' => $tpl), true)) {
                  $template = $chunk->getContent();
      

      $template is a string of the chunk content. Later they simply use str_replace to insert data into the chunk.

      It doesn't seem right that I need to eval a snippet and str_replace a chunk to get them to work correctly but that's the work around that I'm using for now. Also instead of logging in a test user I get the modUserProfile object and assign it to $modx->user.
      • discuss.answer
        • 3749
        • 24,544 Posts
        There are some things to try here (in particular $modx->getRequest() ):

        http://bobsguides.com/blog.html/2013/07/13/using-modx-outside-of-modx/

        You might also try some variant of this:

        $modx->user=null;


        If a user is already logged in, the processor may reject the login request.
          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
          • 45892
          • 9 Posts
          Thank you Bob!

          Setting -
          $modx->user=null;

          solved my login problem. It seems to work consistently now.

          I suspect this will lead to solutions for (at least some of) my other issues.
            • 3749
            • 24,544 Posts
            Glad I could help. Bear in mind that when you're previewing from the Manager, your status is weird. You have a session context and are logged in to the Manager Context, but in the front-end Context, you're not logged in (unless you explicitly log in again) and you're the (anonymous) user. With the default settings, you can also see unpublished resources.
              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