API:makeUrl

From MODx Wiki

Jump to: navigation, search
 API Function Definition: 
makeUrl
Versions 
 ? (0.9.5 for sure :)
Return Values
Success: document's URL
Fail: calls messageQuit() on non-numeric ID
Data Type 
string
Object Hierarchy 
DocumentParser
string makeUrl(int $id[, mixed $alias[, mixed $args[, mixed $scheme]]]);


  • $id - target document ID
  • $alias - document's alias name
  • $args - URL arguments string
  • $scheme - could be "full" for building complete URL with protocol and host

Returns absolute or relative URL of a document, specified by it's numerical ID. Function doesn't check for existence and publication of the document.

Examples

// Retrieve document 4 relative URL.
$url = $modx->makeUrl(4);
 
// Retrieve document 15 complete URL.
$url = $modx->makeUrl(15, '', '', 'full');
 
// Retrieve document 21 with extra query string
$url = $modx->makeUrl(21,'','&page=2&cat=dog');

Related Functions

Function Source

File: manager/includes/document.parser.class.inc.php
Line: 1519
function makeUrl($id, $alias= '', $args= '', $scheme= '') {
        $url= '';
        $virtualDir= '';
        if (!is_numeric($id)) {
            $this->messageQuit('`' . $id . '` is not numeric and may not be passed to makeUrl()');
        }
        if ($args != '' && $this->config['friendly_urls'] == 1) {
            // add ? to $args if missing
            $c= substr($args, 0, 1);
            if (strpos($this->config['friendly_url_prefix'], '?') === false) {
                if ($c == '&')
                    $args= '?' . substr($args, 1);
                elseif ($c != '?') $args= '?' . $args;
            } else {
                if ($c == '?')
                    $args= '&' . substr($args, 1);
                elseif ($c != '&') $args= '&' . $args;
            }
        }
        elseif ($args != '') {
            // add & to $args if missing
            $c= substr($args, 0, 1);
            if ($c == '?')
                $args= '&' . substr($args, 1);
            elseif ($c != '&') $args= '&' . $args;
        }
        if ($this->config['friendly_urls'] == 1 && $alias != '') {
            $url= $this->config['friendly_url_prefix'] . $alias . $this->config['friendly_url_suffix'] . $args;
        }
        elseif ($this->config['friendly_urls'] == 1 && $alias == '') {
            $alias= $id;
            if ($this->config['friendly_alias_urls'] == 1) {
                $al= $this->aliasListing[$id];
                $alPath= !empty ($al['path']) ? $al['path'] . '/' : '';
                if ($al && $al['alias'])
                    $alias= $al['alias'];
            }
            $alias= $alPath . $this->config['friendly_url_prefix'] . $alias . $this->config['friendly_url_suffix'];
            $url= $alias . $args;
        } else {
            $url= 'index.php?id=' . $id . $args;
        }
 
        $host= $this->config['base_url'];
        // check if scheme argument has been set
        if ($scheme != '') {
            // for backward compatibility - check if the desired scheme is different than the current scheme
            if (is_numeric($scheme) && $scheme != $_SERVER['HTTPS']) {
                $scheme= ($_SERVER['HTTPS'] ? 'http' : 'https');
            }
 
            // to-do: check to make sure that $site_url incudes the url :port (e.g. :8080)
            $host= $scheme == 'full' ? $this->config['site_url'] : $scheme . '://' . $_SERVER['HTTP_HOST'] . $this->config['base_url'];
        }
 
        if ($this->config['xhtml_urls']) {
        	return preg_replace("/&(?!amp;)/","&", $host . $virtualDir . $url);
        } else {
        	return $host . $virtualDir . $url;
        }
    }

Notes

if you set the 'alias' parameter, make sure you take into account friendly path. Otherwise, leave empty to let MODx set the right alias.

Personal tools