We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32507
    • 142 Posts
    After moving our site to production server, frontend stopped working. (unless you're logged in as a admin and preview the site)

    This was on Server error log:

    PHP Fatal error: Call to a member function get() on a non-object in /core/cache/includes/elements/modsnippet/50.include.cache.php on line 3, referer: http://...

    After studying server logs we located error in this simple custom snippet.

    <?php
    $page = $modx->getObject('modResource', $resource);
    $getid = $page->get('id');
    if ($getid==24) {
        return 'dropdown-submenu';
    }


    Weird thing: When i login to Manager as an admin and preview some pages, these particular pages starts working for anonymous user too? Snippet above doesn't give an error. After clearing cache the issue returns.

    Is this server side problem, or what could be causing this?

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      Snippets like that always need a sanity check. Calling get() on an object you didn't successfully retrieve will always cause a fatal PHP error:

      <?php
      $page = $modx->getObject('modResource', $resource);
      $getid = null;
      
      if ($page) {
          $getid = $page->get('id');
      }
      if ($getid==24) {
          return 'dropdown-submenu';
      }


      Where is $resource coming from? It should be a Resource ID, but there's no code to set it.
        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
        • 32507
        • 142 Posts
        Thanks! Started to work! I Still doesn't fully understand why it's working on different server without sanity check?

        Resource ID comes from snippet call parameter. Quess resource isn't the best name for parameter?
        [[submenuFilter? &resource=`[[+id]]`]]
          • 3749
          • 24,544 Posts
          &docId would be a good name for it. wink

          In the snippet, this would be good form:

          $docId = $modx->getOption('docId', $scriptProperties, null, true);
          
          if (!empty($docId)) {
              $page = $modx->getObject('modResource', $docId);
              $getid = null;
           
              if ($page) {
                  $getid = $page->get('id');
              }
              if ($getid==24) {
                  return 'dropdown-submenu';
              }
          }
            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