Created
April 27, 2012 15:52
-
-
Save tom--/2510335 to your computer and use it in GitHub Desktop.
hardUrl helper function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generate an external URL with scheme, hostname and etc. under any(?) circumstances | |
* | |
* @param mixed $url Specs for the url, as CHtml::normalizeUrl() would expect | |
* @param string $scheme 'http' or 'https' | |
* @throws CException on missing 'extBaseUrl' app param in non-webapp context | |
* @return string | |
*/ | |
public static function hardUrl($url, $scheme = 'http') { | |
$app = Yii::app(); | |
if (is_array($url)) { | |
if (isset($url[0])) { | |
$creator = $app->getController(); | |
if ($creator === null) { | |
$creator = $app; | |
} | |
$url = $creator->createUrl($url[0], array_splice($url, 1)); | |
} else { | |
$url = ''; | |
} | |
} | |
if ($app instanceof CWebApplication && isset($_SERVER['SERVER_NAME'])) { | |
$request = $app->getRequest(); | |
$base = $request->getHostInfo($scheme) . $request->getBaseUrl(); | |
} else { | |
if (!isset($app->params['extBaseUrl'])) { | |
throw new CException( | |
'Cannot create URL. Set "extBaseUrl" in application params array.' | |
); | |
} else { | |
$base = $app->params['extBaseUrl']; | |
if (isset($base[$scheme])) { | |
$base = $base[$scheme]; | |
} | |
} | |
} | |
return rtrim($base, '/') . '/' . ltrim($url, '/'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment