We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4619
    • 23 Posts

    I followed this documentation. But the current resource seems not loaded after this.
    How could I simply load a resource from the DB as the current resource ($modx->resource).
      • 3749
      • 24,544 Posts
      After running the code in that example, you need to get the resource with $ modx-> getObject().
        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
        • 4619
        • 23 Posts
        When I try the following code:
        <?php
        
        define('MODX_BASE_PATH', '');
        define('MODX_BASE_URL', '/');
        
        require_once MODX_BASE_PATH.'config.core.php';
        
        require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';
        require_once MODX_CORE_PATH.'model/modx/modx.class.php';
        $modx = new modX();
        $modx->initialize('web');
        
        $doc = $modx->getObject('modResource',array(
            'id' => 1
        ));
        
        $id = $modx->resource->get('id');
        
        ?>
        I don’t initialize $modx->resource. So I get "Call to a member function get() on a non-object" error message.
        Which is normal as $getObject doesn’t affect $modx.

        How could I initialize $modx->resource ?
          • 3749
          • 24,544 Posts
          When you are outside of MODx, you initialize MODx but there *is* no current resource (and no reason to set $modx->resource that I can think of offhand).

          Can you explain what you’re trying to do? Are you trying to create a resource, display an existing resource’s content, or what?

          And why do you need to do it outside of MODx?

            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
            • 4619
            • 23 Posts
            And why do you need to do it outside of MODx?
            I test a snippet under phpStorm. First, I have tried to test a document (as url) with the snippet call iincluded on the page (and then an active resource) but for some reasons (wrong configuration, may be), It don’t work.
            So I have set up a test script which run outside of MODx. This script initialize MODx environment and run the snippet.

            Can you explain what you’re trying to do? Are you trying to create a resource, display an existing resource’s content, or what?
            The snippet output some resources (as rendered/ parsed output), but unfortunately Wayfinder (but probably some other addons) initialize by default the current resource as $modx->resource->get(’id’). So I get an error "Call to a member function get() on a non-object" error message.

            I have tried (code part after $modx->initialize(’web’); )
            $modx->initialize('web');
            
            if ($modx->getRequest()) {
                $request = new modRequest($modx);
                $request->getResource('id',1);
            }

            But without success undecided
            • If you are using the MODX request object, you should probably just go ahead and use the MODX handleRequest() method, just like the index.php does. If you want to do your own routing or integrate some simple MODX output into another system, avoid the modRequest class altogether. You’ll have to make sure any content you load externally like this will work as expected without the normal MODX request/response cycle.
                • 3749
                • 24,544 Posts
                It sounds like you are trying to use PhpStorm for debugging a snippet. One way to do that is to:

                * Create a PhpStorm project that contains your whole MODx install,

                * Put the snippet code in an external file and use this for the snippet code in MODx:
                        return include "path/to/snippet/file";


                * In PhpStorm, put a breakpoint in the snippet file.

                * In PhpStorm, run debug by launching the index.php file in the MODx root directory.

                Doing it this way is a little slow, but you won’t have to modify the snippet code at all to make it work.
                  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