We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30107
    • 44 Posts
    I'm working on some kind of static cache/staging plugin for MODX, based on the statCache plugin by opengeek. In order to only clear parts of the static cache when saving a ressource, I need to know the relative path of the ressource. I'm using the event "OnDocFormSave", and hope to get the path by $modx->makeUrl().

    makeUrl (integer $id, [string $context = ''], [string $args = ''], [mixed $scheme = -1])

    The docs say that $scheme can be

    -1 : (default value) URL is relative to site_url
    0 : see http
    1 : see https
    full : URL is absolute, prepended with site_url from config
    abs : URL is absolute, prepended with base_url from config
    http : URL is absolute, forced to http scheme
    https : URL is absolute, forced to https scheme

    But whatever I choose for $scheme, the output of makeUrl is always "http://www.mydomain.com/mypath/myresource". It never gets relative. Is this an intended feature?

    What's the most elegant way to get the relative path of the ressource? If I think I have to get the host name and do a substr thing... shabby ;-)

    This question has been answered by multiple community members. See the first response.

    • Are you looking at the actual output of the makeURL function, or are you looking at the final link's URL in a web browser? Your base tag in your page head will automatically add the leading part of the URL.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
      • discuss.answer
        • 30107
        • 44 Posts
        Quote from: sottwell at Nov 23, 2012, 12:14 PM
        Are you looking at the actual output of the makeURL function, or are you looking at the final link's URL in a web browser? Your base tag in your page head will automatically add the leading part of the URL.
        I'm looking at the actual output of the makeUrl() function, written to log with
        $url = $modx->makeUrl($resource->get('id'));
        $modx->log(MODX_LOG_LEVEL_ERROR, 'URL: ' . $url);
          • 33968
          • 863 Posts
          As you're running the plugin from the 'mgr' context, did you specify 'web' in the $context parameter?
            • 30107
            • 44 Posts
            Quote from: okyanet at Nov 23, 2012, 12:28 PM
            As you're running the plugin from the 'mgr' context, did you specify 'web' in the $context parameter?
            Interesting hint. Tried that a few seconds ago.

            case 'OnDocFormSave':
            $url = $modx->makeUrl($resource->get('id'), 'web');
            $modx->log(modX::LOG_LEVEL_ERROR, 'URL: ' . $url, '', __FUNCTION__, __FILE__, __LINE__);

            In fact, I even tried all possible combinations:
            $modx->makeUrl($resource->get('id'), 'web', '', -1);
            $modx->makeUrl($resource->get('id'), 'web', '', 0);
            $modx->makeUrl($resource->get('id'), 'web', '', 1);
            $modx->makeUrl($resource->get('id'), 'web', '', 'full');
            $modx->makeUrl($resource->get('id'), 'web', '', 'abs');
            $modx->makeUrl($resource->get('id'), 'web', '', 'http');
            $modx->makeUrl($resource->get('id'), 'web', '', 'https');

            ...but the result is always the same:
            URL: http://www.mydomain.com/mypath/myresource [ed. note: titanium last edited this post 11 years, 5 months ago.]
            • discuss.answer
              • 33968
              • 863 Posts
              You could also try $modx->switchContext('web') just before your makeUrl() call (not sure if that will work). Switch back to 'mgr' afterwards.

              I think I remember cross-context urls always being forced to absolute scheme.
                • 30107
                • 44 Posts
                Quote from: okyanet at Nov 23, 2012, 12:56 PM
                You could also try $modx->switchContext('web') just before your makeUrl() call (not sure if that will work). Switch back to 'mgr' afterwards.

                I think I remember cross-context urls always being forced to absolute scheme.
                Thanks a lot, it's working!

                $context = $modx->context->key; // results in "mgr"

                $modx->switchContext('web');
                $context = $modx->context->key; // results in "web"

                $url = $modx->makeUrl($resource->get('id')); // url path is relative now! Yeah!

                $modx->switchContext('mgr');
                $context = $modx->context->key; // results back to "mgr"

                Great!
                  • 33968
                  • 863 Posts
                  Glad it works - although it's still not a very pretty solution.

                  Here's one of my posts on this subject from over a year ago, and OpenGeek's explanation. I still don't agree with cross-context links being forced to absolute, that should be the developer's choice.

                  If you could support my feature request for a system setting to determine this, that would be great:
                  http://tracker.modx.com/issues/5999
                    • 1120
                    • 13 Posts
                    Quote from: titanium at Nov 23, 2012, 08:24 AM
                    Quote from: okyanet at Nov 23, 2012, 12:56 PM
                    You could also try $modx->switchContext('web') just before your makeUrl() call (not sure if that will work). Switch back to 'mgr' afterwards.

                    I think I remember cross-context urls always being forced to absolute scheme.
                    Thanks a lot, it's working!

                    $context = $modx->context->key; // results in "mgr"

                    $modx->switchContext('web');
                    $context = $modx->context->key; // results in "web"

                    $url = $modx->makeUrl($resource->get('id')); // url path is relative now! Yeah!

                    $modx->switchContext('mgr');
                    $context = $modx->context->key; // results back to "mgr"

                    Great!

                    go - manager/contenxt, next default context (example web) and create parameter site_url — http://domain.name
                      • 3749
                      • 24,544 Posts
                      I'm not sure if this helps you or not, but the recommended form of makeUrl() is:


                      $url = $modx->makeUrl($resource->get('id'), "", "", "full");



                        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