From d09c51afac9df77a52380dc2ac6581a104faa2ff Mon Sep 17 00:00:00 2001 From: "anaxamaxan@blackdog.local" Date: Wed, 2 Feb 2011 23:00:16 -0800 Subject: Made config->base_url() accept parameters. config->base_url($uri). Updated base_url() in url_helper accordingly. --- system/core/Config.php | 58 ++++++++++++++++++++++++++++++++++--------- system/helpers/url_helper.php | 11 +++++--- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/system/core/Config.php b/system/core/Config.php index da22222dc..554b7360a 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -194,10 +194,7 @@ class CI_Config { // -------------------------------------------------------------------- /** - * Fetch a config file item - adds slash after item - * - * The second parameter allows a slash to be added to the end of - * the item, in the case of a path. + * Fetch a config file item - adds slash after item (if item is not empty) * * @access public * @param string the config item name @@ -210,6 +207,10 @@ class CI_Config { { return FALSE; } + if( trim($this->config[$item]) == '') + { + return ''; + } return rtrim($this->config[$item], '/').'/'; } @@ -218,6 +219,7 @@ class CI_Config { /** * Site URL + * Returns base_url . index_page [. uri_string] * * @access public * @param string the URI string @@ -230,16 +232,50 @@ class CI_Config { return $this->slash_item('base_url').$this->item('index_page'); } + if ($this->item('enable_query_strings') == FALSE) + { + $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix'); + return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix; + } + else + { + return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri); + } + } + + // ------------------------------------------------------------- + + /** + * Base URL + * Returns base_url [. uri_string] + * + * @access public + * @param string $uri + * @return string + */ + function base_url($uri = '') + { + return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/'); + } + + // ------------------------------------------------------------- + + /** + * Build URI string for use in Config::site_url() and Config::base_url() + * + * @access protected + * @param $uri + * @return string + */ + protected function _uri_string($uri) + { if ($this->item('enable_query_strings') == FALSE) { if (is_array($uri)) { $uri = implode('/', $uri); } - - $index = $this->item('index_page') == '' ? '' : $this->slash_item('index_page'); - $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix'); - return $this->slash_item('base_url').$index.trim($uri, '/').$suffix; + $uri = trim($uri, '/'); } else { @@ -253,16 +289,14 @@ class CI_Config { $str .= $prefix.$key.'='.$val; $i++; } - $uri = $str; } - - return $this->slash_item('base_url').$this->item('index_page').'?'.$uri; } + return $uri; } // -------------------------------------------------------------------- - + /** * System URL * diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php index d0516cee6..9f4b85248 100644 --- a/system/helpers/url_helper.php +++ b/system/helpers/url_helper.php @@ -50,18 +50,21 @@ if ( ! function_exists('site_url')) /** * Base URL - * - * Returns the "base_url" item from your config file + * + * Create a local URL based on your basepath. + * Segments can be passed in as a string or an array, same as site_url + * or a URL to a file can be passed in, e.g. to an image file. * * @access public + * @param string * @return string */ if ( ! function_exists('base_url')) { - function base_url() + function base_url($uri = '') { $CI =& get_instance(); - return $CI->config->slash_item('base_url'); + return $CI->config->base_url($uri); } } -- cgit v1.2.3-24-g4f1b From 76696d76e137e98f0597547b71b40a991d8b025b Mon Sep 17 00:00:00 2001 From: "anaxamaxan@blackdog.local" Date: Wed, 2 Feb 2011 23:09:54 -0800 Subject: Added docs for config->base_url() changes. --- user_guide/helpers/url_helper.html | 16 +++++++++++++++- user_guide/libraries/config.html | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/user_guide/helpers/url_helper.html b/user_guide/helpers/url_helper.html index 6d8bdc240..de28a6f56 100644 --- a/user_guide/helpers/url_helper.html +++ b/user_guide/helpers/url_helper.html @@ -70,7 +70,7 @@ URL Helper

site_url()

Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your -site index_page in your config file) will be added to the URL, as will any URI segments you pass to the function.

+site index_page in your config file) will be added to the URL, as will any URI segments you pass to the function, and the url_suffix as set in your config file.

You are encouraged to use this function any time you need to generate a local URL so that your pages become more portable in the event your URL changes.

@@ -93,6 +93,20 @@ echo site_url($segments);

Returns your site base URL, as specified in your config file. Example:

echo base_url(); +

This function returns the same thing as site_url, without the index_page or url_suffix being appended.

+ +

Also like site_url, you can supply segments as a string or an array. Here is a string example:

+ +echo base_url("blog/post/123"); + +

The above example would return something like: http://example.com/blog/post/123

+ +

This is useful because unlike site_url(), you can supply a string to a file, such as an image or stylesheet. For example:

+ +echo base_url("images/icons/edit.png"); + +

This would give you something like: http://example.com/images/icons/edit.png

+

current_url()

Returns the full URL (including segments) of the page being currently viewed.

diff --git a/user_guide/libraries/config.html b/user_guide/libraries/config.html index 98b6052a9..103161f5d 100644 --- a/user_guide/libraries/config.html +++ b/user_guide/libraries/config.html @@ -175,6 +175,11 @@ define('ENVIRONMENT', 'development');

$this->config->site_url();

This function retrieves the URL to your site, along with the "index" value you've specified in the config file.

+

$this->config->base_url();

+

This function retrieves the URL to your site, plus an optional path such as to a stylesheet or image.

+ +

The two functions above are normally accessed via the corresponding functions in the URL Helper.

+

$this->config->system_url();

This function retrieves the URL to your system folder.

-- cgit v1.2.3-24-g4f1b