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

    Forgive me if this has been answered already on here but I cant find it by searching. I’m trying to simply load up a modx chunk from an separate php file using the following:

    define('MODX_CORE_PATH', '/path/to/modx/core/');
    define('MODX_CONFIG_KEY', 'config');
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    $modx= new modX();
    $modx->initialize('mgr');
    
    $chunk = $modx->getObject('modChunk',array('name' => 'Header' ));
    
    echo $chunk;
    


    But it’s not working, is this to do with contexts? or do I need to switch on api mode or something? please help.

    Many thanks
    Bevan
      • 21257 MODX Staff
      • 730 Posts
      The biggest problem with your example code is that $chunk will be an object, so you’re echoing an object.
      Try
      echo $chunk->getContent();
      


      You might want to initialize the ’web’ context instead of ’mgr’.
      And I think it should be in API mode as well.
      define('MODX_API_MODE', true);
      

        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 3241
        • 8 Posts
        Ahaaa, of course, what an idiot, many thanks!
          • 34017
          • 898 Posts
          Has there been thoughts on creating a __toString method on modResource?
            Chuck the Trukk
            ProWebscape.com :: Nashville-WebDesign.com
            - - - - - - - -
            What are TV's? Here's some info below.
            http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
            http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
            • 12241
            • 80 Posts
            This works in Modx Revolution! (with tweaking needed)

            Found this on Tim Spencer’s blog: http://timspencerweb.co.uk/blog/?p=3#comment-867

            I’m using it, with edits to work with Modx Revolution 2.0.4 on a custom external php page (photo gallery script) that is located in a subdirectory (/modx/core/compenents/myGallery/gallery-name/index.php) and it is displaying the contents of a modx chunk titled “header” correctly including the output of the Wayfinder menu.

            But there’s a problem with the Wayfinder generated URLs.
            The links in the menu are all written with the full directory path like this:

            localhost/modx/core/compenents/myGallery/gallery-name/index.php?id=1

            ..should be:

            localhost/modx/index.php?id=1

            Any idea how to fix it?

            Here is the code (found in ’/modx/core/compenents/myGallery/gallery-name/index.php’):
            //modx object - test
            require_once ‘../../../../../manager/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’);
            
            //tim’s code:
            define(’MODX_SITE_BASE_URL’, ‘http://localhost/site/modx/’);
            
            // The MODx document we are going to pretend to be
            define(’MODX_DOCUMENT_IDENTIFIER’, 48);
            
            // Some defines used by the modx API
            define(’MODX_API_MODE’, true);
            define(’MODX_SITE_URL’, (!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) == ‘off’ ? ‘http://’ : ‘https://’).$_SERVER['SERVER_NAME'].MODX_SITE_BASE_URL);
            
            // Run the MODx config and get site settings
            require(MODX_MANAGER_PATH.’/../index.php’);
            $modx->getSettings();
            
            // Set the docid (most sensibly to the MODx weblink to here) and get the MODx document object
            $modx->documentObject = $modx->getDocumentObject(’id’, MODX_DOCUMENT_IDENTIFIER); $modx->documentIdentifier = MODX_DOCUMENT_IDENTIFIER
            
            // Set the base URL for any MODx snippets
            $modx->config['base_url'] = MODX_SITE_BASE_URL;
            //END Tim’s code
            
            //DISPLAY HEADER CHUNK W/ NAVIGATION
            $chunk = $modx->getChunk(’header’) ;
            if($chunk){echo($chunk);}
            
            //—————————
            


            NOTE:
            I found that when I add the <base href="site_url"> to the page, like so:
            /* gets the site_url setting */
            $site_url = $modx->getOption('site_url');
            
            //then add this in the pages <head>
            <base href="<?php echo($site_url);?>" />
            
            

            it fixes the links that Wayfinder creates, However the rest of the page breaks because the file is in a different directory.
              Environment:
              modx: rev 2.0.8-pl
              localhost: Apache/2.0.63 (Unix) PHP/5.2.11 DAV/2, MySQL client version: 5.1.37
              • 22303 MODX Staff
              • 10,725 Posts
              Can I ask why this is being done this way instead of taking the PHP from the external page and converting it to a Snippet? It doesn’t make any sense to me to use MODx like this as you lose all of the benefits of partial Resource caching and other important considerations. Plus, as you can tell, you’ll need to manipulate the paths; MODx expects to always work from the root of the site unless you define a context and set the appropriate settings to override it.
                • 12241
                • 80 Posts
                taking the PHP from the external page and converting it to a Snippet

                The PHP is an external gallery application that creates directories in the file system for each gallery that also contain their own index.php file for displaying the gallery. I’m not sure how to convert it to a modx snippet but it did cross my mind that this is the best way to integrate with modx. It also crossed my mind that it would be easier to just load modx on each of those gallery index.php pages.

                I also considered just putting the whole thing in an iFrame and calling it a day:) But it really is a lame way to do it and I need to have more control over the design and functionality than what an iframe provides.

                So... back to the drawing board, next task: learn how to convert external php app into modx snippet...

                Or
                MODx expects to always work from the root of the site unless you define a context and set the appropriate settings to override it.
                This would work, but the index.php files are externally located in each gallery folder so the location will be different for each gallery.... does this mean MODx will need a separate context for each external index.php file?
                  Environment:
                  modx: rev 2.0.8-pl
                  localhost: Apache/2.0.63 (Unix) PHP/5.2.11 DAV/2, MySQL client version: 5.1.37
                  • 22303 MODX Staff
                  • 10,725 Posts
                  You could create a custom context with it’s own modRequest and/or modResponse derivatives to handle requests to these pages, but IMO, it would make more sense to convert it to MODx and use it within the infrastructure so you can take advantage of the routing, caching, and more.