Last active
February 3, 2016 17:49
-
-
Save javierarques/2051aa4beb0cfcc422a3 to your computer and use it in GitHub Desktop.
twig function to generate absolute URL's
This file contains hidden or 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
/** | |
* 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