Skip to content

Instantly share code, notes, and snippets.

@javierarques
Last active February 3, 2016 17:49
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 javierarques/2051aa4beb0cfcc422a3 to your computer and use it in GitHub Desktop.
Save javierarques/2051aa4beb0cfcc422a3 to your computer and use it in GitHub Desktop.
twig function to generate absolute URL's
/**
* absoluteUrl
* twig function to generate absolute URL's
*
* @param {string} $string fixed slug appended to Url
* @param {array} [$params] array with query string params to add to the Url
*
*/
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$twig->addFunction(new \Twig_SimpleFunction('absoluteUrl', function ($string, $params = []) use ($app) {
$queryParams = $app['request']->query->all();
$queryParams = array_merge($queryParams, $params);
$queryString = http_build_query($queryParams);
if ($queryString) {
$queryString = '?' . $queryString;
}
return
$app["request"]->getSchemeAndHttpHost() .
$app["request"]->getBaseURL() .
$app["request"]->getPathInfo() .
$string .
$queryString;
}));
return $twig;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment