From b7b80d6d49aaecfdc74f6b04f46ec9e4aa07b59d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 28 Nov 2011 23:10:16 -0600 Subject: [PATCH] the application url will now be auto-detected. --- application/config/application.php | 2 +- laravel/uri.php | 2 +- laravel/url.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/application/config/application.php b/application/config/application.php index 644dd4b6..1c112ad6 100644 --- a/application/config/application.php +++ b/application/config/application.php @@ -11,7 +11,7 @@ | */ - 'url' => 'http://localhost', + 'url' => '', /* |-------------------------------------------------------------------------- diff --git a/laravel/uri.php b/laravel/uri.php index a4fc6936..ca7b611b 100644 --- a/laravel/uri.php +++ b/laravel/uri.php @@ -34,7 +34,7 @@ public static function current() // Remove the root application URL from the request URI. If the application // is nested within a sub-directory of the web document root, this will get // rid of all of the the sub-directories from the request URI. - $uri = static::remove($uri, parse_url(Config::$items['application']['url'], PHP_URL_PATH)); + $uri = static::remove($uri, parse_url(URL::base(), PHP_URL_PATH)); if (($index = '/'.Config::$items['application']['index']) !== '/') { diff --git a/laravel/url.php b/laravel/url.php index 15cb25c4..cb2f6125 100644 --- a/laravel/url.php +++ b/laravel/url.php @@ -2,6 +2,35 @@ class URL { + /** + * Get the base URL of the application. + * + * If the application URL is set in the application configuration file, that + * URL will be returned. Otherwise, the URL will be guessed based on the + * server variables available to the script in the $_SERVER array. + * + * @return string + */ + public static function base() + { + if (($base = Config::$items['application']['url']) !== '') return $base; + + if (isset($_SERVER['HTTP_HOST'])) + { + $protocol = (Request::secure()) ? 'https://' : 'http://'; + + // By removing the basename of the script, we should be left with the path + // in which the framework is installed. For example, if the framework is + // installed to http://localhost/laravel/public, the path we'll get from + // from this statement will be "/laravel/public". + $path = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); + + return rtrim($protocol.$_SERVER['HTTP_HOST'].$path, '/'); + } + + return 'http://localhost'; + } + /** * Generate an application URL. *