We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34926
    • 87 Posts
    I have a plugin that uses makeUrl when a document gets saved.
    $modx->makeUrl($doc['id'])

    This works fine in multiple contexts, with and without friendly URLs enabled.

    My problem is that not all MODx installations have a site_url set. Because of that I am using this method to determine if a hostname is present and act accordingly.

    protected function form_url($url = NULL)
    {
    	$host = FALSE;
    
    	if($parsed = parse_url($url))
    	{
    		if($parsed['host'])
    			$host = TRUE;
    	}
    
    	// FIXME: Is SERVER_NAME reliable enough?
    	return $url = ($host) ? $url : 'http://' . $_SERVER['SERVER_NAME'] . '/' . $url;
    }


    The last line is what I am concerned about - its crap. Does anyone have any suggestions or tips on how to approach this? Note: $url is actually the alias returned by makeUrl.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Every site has MODX_SITE_URL defined in the config.inc.php file.
        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
        • 34926
        • 87 Posts
        Ah I knew I was missing a trick. Thanks Susan!
          • 33968
          • 863 Posts
          You can also get it using the api:
          // site url, eg. http://www.domain.com/
          $siteUrl = $modx->getOption('site_url');
          
          // http_host, eg. www.domain.com
          $httpHost = $modx->getOption('http_host');
          
            • 34926
            • 87 Posts
            Quote from: okyanet at Sep 10, 2012, 08:16 PM
            You can also get it using the api:
            // site url, eg. http://www.domain.com/
            $siteUrl = $modx->getOption('site_url');
            
            // http_host, eg. www.domain.com
            $httpHost = $modx->getOption('http_host');
            

            I tried this but on a few installations it wouldn't return anything.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              There are certain server configurations where they don't provide some of the expected values for the $_SERVER array. I've run across them a few times myself. You may actually have to manually edit the config.inc.php file to supply the known value. That means the site isn't very portable, but at least it works.
                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