Skip to content

Commit

Permalink
add default host in get_url helper
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Sep 13, 2016
1 parent 7cc303a commit 49d7154
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions api/core/functions.php
Expand Up @@ -143,13 +143,16 @@ function is_ssl()
* Get Directus URL
*
* @param $path - Extra path to add to the url
* @param $defaultHost
*
* @return string
*/
function get_url($path = '')
function get_url($path = '', $defaultHost = 'localhost')
{
$schema = is_ssl() ? 'https://' : 'http://';
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $defaultHost;
$directusPath = defined('DIRECTUS_PATH') ? DIRECTUS_PATH : '/';
$directusHost = rtrim($_SERVER['HTTP_HOST'] . $directusPath, '/') . '/';
$directusHost = rtrim($host . $directusPath, '/') . '/';

return $schema . $directusHost . ltrim($path, '/');
}
Expand Down
6 changes: 6 additions & 0 deletions tests/api/functionsTest.php
Expand Up @@ -22,6 +22,12 @@ public function testSSL()

public function testGetURL()
{
$url = get_url();
$this->assertSame('http://localhost/', $url);

$url = get_url('/users', 'example.local');
$this->assertSame('http://example.local/users', $url);

$_SERVER['HTTP_HOST'] = 'localhost';

$url = get_url();
Expand Down

0 comments on commit 49d7154

Please sign in to comment.