Skip to content

Instantly share code, notes, and snippets.

@tom--
Created April 27, 2012 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom--/2510335 to your computer and use it in GitHub Desktop.
Save tom--/2510335 to your computer and use it in GitHub Desktop.
hardUrl helper function
<?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