diff options
-rw-r--r-- | system/core/Config.php | 58 | ||||
-rw-r--r-- | system/helpers/url_helper.php | 11 | ||||
-rw-r--r-- | user_guide/helpers/url_helper.html | 66 | ||||
-rw-r--r-- | user_guide/libraries/config.html | 5 |
4 files changed, 98 insertions, 42 deletions
diff --git a/system/core/Config.php b/system/core/Config.php index fa71f4d3d..d871f5432 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -203,10 +203,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 @@ -219,6 +216,10 @@ class CI_Config { { return FALSE; } + if( trim($this->config[$item]) == '') + { + return ''; + } return rtrim($this->config[$item], '/').'/'; } @@ -227,6 +228,7 @@ class CI_Config { /** * Site URL + * Returns base_url . index_page [. uri_string] * * @access public * @param string the URI string @@ -241,14 +243,48 @@ class CI_Config { 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 { @@ -262,16 +298,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 8e93744fb..d2fa47074 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); } } diff --git a/user_guide/helpers/url_helper.html b/user_guide/helpers/url_helper.html index 0bcf1e7ae..de28a6f56 100644 --- a/user_guide/helpers/url_helper.html +++ b/user_guide/helpers/url_helper.html @@ -27,7 +27,7 @@ <div id="masthead"> <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> <tr> -<td><h1>CodeIgniter User Guide Version 2.0.2</h1></td> +<td><h1>CodeIgniter User Guide Version 2.0.0</h1></td> <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> </tr> </table> @@ -69,13 +69,13 @@ URL Helper <h2>site_url()</h2> -<p>Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your -site <dfn>index_page</dfn> in your config file) will be added to the URL, as will any URI segments you pass to the function.</p> +<p>Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your +site <dfn>index_page</dfn> in your config file) will be added to the URL, as will any URI segments you pass to the function, and the <dfn>url_suffix</dfn> as set in your config file.</p> <p>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.</p> -<p>Segments can be optionally passed to the function as a string or an array. Here is a string example:</p> +<p>Segments can be optionally passed to the function as a string or an array. Here is a string example:</p> <code>echo site_url("news/local/123");</code> @@ -90,16 +90,30 @@ echo site_url($segments);</code> <h2>base_url()</h2> -<p>Returns your site base URL, as specified in your config file. Example:</p> +<p>Returns your site base URL, as specified in your config file. Example:</p> <code>echo base_url();</code> +<p>This function returns the same thing as site_url, without the <dfn>index_page</dfn> or <dfn>url_suffix</dfn> being appended.</p> + +<p>Also like site_url, you can supply segments as a string or an array. Here is a string example:</p> + +<code>echo base_url("blog/post/123");</code> + +<p>The above example would return something like: http://example.com/blog/post/123</p> + +<p>This is useful because unlike site_url(), you can supply a string to a file, such as an image or stylesheet. For example:</p> + +<code>echo base_url("images/icons/edit.png");</code> + +<p>This would give you something like: http://example.com/images/icons/edit.png</p> + <h2>current_url()</h2> <p>Returns the full URL (including segments) of the page being currently viewed.</p> <h2>uri_string()</h2> -<p>Returns the URI segments of any page that contains this function. For example, if your URL was this:</p> +<p>Returns the URI segments of any page that contains this function. For example, if your URL was this:</p> <code>http://some-site.com/blog/comments/123</code> <p>The function would return:</p> @@ -107,7 +121,7 @@ echo site_url($segments);</code> <h2>index_page()</h2> -<p>Returns your site "index" page, as specified in your config file. Example:</p> +<p>Returns your site "index" page, as specified in your config file. Example:</p> <code>echo index_page();</code> @@ -122,15 +136,15 @@ echo site_url($segments);</code> <code>anchor(<var>uri segments</var>, <var>text</var>, <var>attributes</var>)</code> -<p>The first parameter can contain any segments you wish appended to the URL. As with the <dfn>site_url()</dfn> function above, +<p>The first parameter can contain any segments you wish appended to the URL. As with the <dfn>site_url()</dfn> function above, segments can be a string or an array.</p> -<p><strong>Note:</strong> If you are building links that are internal to your application do not include the base URL (http://...). This +<p><strong>Note:</strong> If you are building links that are internal to your application do not include the base URL (http://...). This will be added automatically from the information specified in your config file. Include only the URI segments you wish appended to the URL.</p> -<p>The second segment is the text you would like the link to say. If you leave it blank, the URL will be used.</p> +<p>The second segment is the text you would like the link to say. If you leave it blank, the URL will be used.</p> -<p>The third parameter can contain a list of attributes you would like added to the link. The attributes can be a simple string or an associative array.</p> +<p>The third parameter can contain a list of attributes you would like added to the link. The attributes can be a simple string or an associative array.</p> <p>Here are some examples:</p> @@ -148,7 +162,7 @@ will be added automatically from the information specified in your config file. <p>Nearly identical to the <dfn>anchor()</dfn> function except that it opens the URL in a new window. You can specify JavaScript window attributes in the third parameter to control how the window is opened. If -the third parameter is not set it will simply open a new window with your own browser settings. Here is an example +the third parameter is not set it will simply open a new window with your own browser settings. Here is an example with attributes:</p> <code> @@ -173,7 +187,7 @@ If you want the function to use all of its defaults simply pass an empty array i <h2>mailto()</h2> -<p>Creates a standard HTML email link. Usage example:</p> +<p>Creates a standard HTML email link. Usage example:</p> <code>echo mailto('me@my-site.com', 'Click Here to Contact Me');</code> @@ -188,12 +202,12 @@ written with JavaScript to help prevent the email address from being harvested b <h2>auto_link()</h2> -<p>Automatically turns URLs and email addresses contained in a string into links. Example:</p> +<p>Automatically turns URLs and email addresses contained in a string into links. Example:</p> <code>$string = auto_link($string);</code> -<p>The second parameter determines whether URLs and emails are converted or just one or the other. Default behavior is both -if the parameter is not specified. Email links are encoded as safe_mailto() as shown above.</p> +<p>The second parameter determines whether URLs and emails are converted or just one or the other. Default behavior is both +if the parameter is not specified. Email links are encoded as safe_mailto() as shown above.</p> <p>Converts only URLs:</p> <code>$string = auto_link($string, 'url');</code> @@ -201,42 +215,42 @@ if the parameter is not specified. Email links are encoded as safe_mailto() as s <p>Converts only Email addresses:</p> <code>$string = auto_link($string, 'email');</code> -<p>The third parameter determines whether links are shown in a new window. The value can be TRUE or FALSE (boolean):</p> +<p>The third parameter determines whether links are shown in a new window. The value can be TRUE or FALSE (boolean):</p> <code>$string = auto_link($string, 'both', TRUE);</code> <h2>url_title()</h2> <p>Takes a string as input and creates a human-friendly URL string. This is useful if, for example, you have a blog -in which you'd like to use the title of your entries in the URL. Example:</p> +in which you'd like to use the title of your entries in the URL. Example:</p> <code>$title = "What's wrong with CSS?";<br /> <br /> $url_title = url_title($title);<br /> <br /> -// Produces: Whats-wrong-with-CSS +// Produces: Whats-wrong-with-CSS </code> -<p>The second parameter determines the word delimiter. By default dashes are used. Options are: <dfn>dash</dfn>, or <dfn>underscore</dfn>:</p> +<p>The second parameter determines the word delimiter. By default dashes are used. Options are: <dfn>dash</dfn>, or <dfn>underscore</dfn>:</p> <code>$title = "What's wrong with CSS?";<br /> <br /> $url_title = url_title($title, 'underscore');<br /> <br /> -// Produces: Whats_wrong_with_CSS +// Produces: Whats_wrong_with_CSS </code> -<p>The third parameter determines whether or not lowercase characters are forced. By default they are not. Options are boolean <dfn>TRUE</dfn>/<dfn>FALSE</dfn>:</p> +<p>The third parameter determines whether or not lowercase characters are forced. By default they are not. Options are boolean <dfn>TRUE</dfn>/<dfn>FALSE</dfn>:</p> <code>$title = "What's wrong with CSS?";<br /> <br /> $url_title = url_title($title, 'underscore', TRUE);<br /> <br /> -// Produces: whats_wrong_with_css +// Produces: whats_wrong_with_css </code> <h3>prep_url()</h3> -<p>This function will add <kbd>http://</kbd> in the event that a scheme is missing from a URL. Pass the URL string to the function like this:</p> +<p>This function will add <kbd>http://</kbd> in the event that a scheme is missing from a URL. Pass the URL string to the function like this:</p> <code> $url = "example.com";<br /><br /> $url = prep_url($url);</code> @@ -250,7 +264,7 @@ $url = prep_url($url);</code> to the controller you want to direct to will create the link. The function will build the URL based on your config file values.</p> <p>The optional second parameter allows you to choose between the "location" -method (default) or the "refresh" method. Location is faster, but on Windows servers it can sometimes be a problem. The optional third parameter allows you to send a specific HTTP Response Code - this could be used for example to create 301 redirects for search engine purposes. The default Response Code is 302. The third parameter is <em>only</em> available with 'location' redirects, and not 'refresh'. Examples:</p> +method (default) or the "refresh" method. Location is faster, but on Windows servers it can sometimes be a problem. The optional third parameter allows you to send a specific HTTP Response Code - this could be used for example to create 301 redirects for search engine purposes. The default Response Code is 302. The third parameter is <em>only</em> available with 'location' redirects, and not 'refresh'. Examples:</p> <code>if ($logged_in == FALSE)<br /> {<br /> @@ -262,7 +276,7 @@ redirect('/article/13', 'location', 301);</code> <p class="important"><strong>Note:</strong> In order for this function to work it must be used before anything is outputted to the browser since it utilizes server headers.<br /> -<strong>Note:</strong> For very fine grained control over headers, you should use the <a href="../libraries/output.html">Output Library</a>'s set_header() function.</p> +<strong>Note:</strong> For very fine grained control over headers, you should use the <a href="../libraries/output.html">Output Library</a>'s set_header() function.</p> diff --git a/user_guide/libraries/config.html b/user_guide/libraries/config.html index c0192ca0a..ae159b675 100644 --- a/user_guide/libraries/config.html +++ b/user_guide/libraries/config.html @@ -194,6 +194,11 @@ $site_name = $blog_config['site_name'];</code> <h2>$this->config->site_url();</h2> <p>This function retrieves the URL to your site, along with the "index" value you've specified in the config file.</p> +<h2>$this->config->base_url();</h2> +<p>This function retrieves the URL to your site, plus an optional path such as to a stylesheet or image.</p> + +<p>The two functions above are normally accessed via the corresponding functions in the <a href="../helpers/url_helper.html">URL Helper.</a></p> + <h2>$this->config->system_url();</h2> <p>This function retrieves the URL to your <dfn>system folder</dfn>.</p> |