Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########## |
| 2 | URL Helper |
| 3 | ########## |
| 4 | |
| 5 | The URL Helper file contains functions that assist in working with URLs. |
| 6 | |
| 7 | .. contents:: Page Contents |
| 8 | |
| 9 | Loading this Helper |
| 10 | =================== |
| 11 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 12 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 13 | |
| 14 | $this->load->helper('url'); |
| 15 | |
| 16 | The following functions are available: |
| 17 | |
| 18 | site_url() |
| 19 | ========== |
| 20 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 21 | .. php:function:: site_url($uri = '') |
| 22 | |
| 23 | :param string $uri: URI string |
| 24 | :returns: string |
| 25 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | Returns your site URL, as specified in your config file. The index.php |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 27 | file (or whatever you have set as your site **index_page** in your config |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | file) will be added to the URL, as will any URI segments you pass to the |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 29 | function, plus the **url_suffix** as set in your config file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
| 31 | You are encouraged to use this function any time you need to generate a |
| 32 | local URL so that your pages become more portable in the event your URL |
| 33 | changes. |
| 34 | |
| 35 | Segments can be optionally passed to the function as a string or an |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 36 | array. Here is a string example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 37 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 38 | echo site_url('news/local/123'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 39 | |
| 40 | The above example would return something like: |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 41 | *http://example.com/index.php/news/local/123* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 43 | Here is an example of segments passed as an array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
| 45 | $segments = array('news', 'local', '123'); |
| 46 | echo site_url($segments); |
| 47 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 48 | This function is an alias for ``CI_Config::site_url()``. For more info, |
| 49 | please see the :doc:`Config Library <../libraries/config>` documentation. |
| 50 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | base_url() |
| 52 | =========== |
| 53 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 54 | .. php:function:: base_url($uri = '') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 56 | :param string $uri: URI string |
| 57 | :returns: string |
| 58 | |
| 59 | Returns your site base URL, as specified in your config file. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | |
| 61 | echo base_url(); |
| 62 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 63 | This function returns the same thing as :php:func:`site_url()`, without |
| 64 | the *index_page* or *url_suffix* being appended. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 66 | Also like :php:func:`site_url()`, you can supply segments as a string or |
| 67 | an array. Here is a string example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | |
| 69 | echo base_url("blog/post/123"); |
| 70 | |
| 71 | The above example would return something like: |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 72 | *http://example.com/blog/post/123* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 74 | This is useful because unlike :php:func:`site_url()`, you can supply a |
| 75 | string to a file, such as an image or stylesheet. For example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
| 77 | echo base_url("images/icons/edit.png"); |
| 78 | |
| 79 | This would give you something like: |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 80 | *http://example.com/images/icons/edit.png* |
| 81 | |
| 82 | This function is an alias for ``CI_Config::base_url()``. For more info, |
| 83 | please see the :doc:`Config Library <../libraries/config>` documentation. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
| 85 | current_url() |
| 86 | ============= |
| 87 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 88 | .. php:function:: current_url() |
| 89 | |
| 90 | :returns: string |
| 91 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | Returns the full URL (including segments) of the page being currently |
| 93 | viewed. |
| 94 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 95 | .. note:: Calling this function is the same as doing this: |
| 96 | | |
| 97 | | site_url(uri_string()); |
| 98 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 99 | uri_string() |
| 100 | ============ |
| 101 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 102 | .. php:function:: uri_string() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 104 | :returns: string |
| 105 | |
| 106 | Returns the URI segments of any page that contains this function. |
| 107 | For example, if your URL was this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
| 109 | http://some-site.com/blog/comments/123 |
| 110 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 111 | The function would return:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
Melvin Lammerts | 3b1e6e6 | 2013-01-23 22:46:16 +0100 | [diff] [blame] | 113 | blog/comments/123 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 115 | This function is an alias for ``CI_Config::uri_string()``. For more info, |
| 116 | please see the :doc:`Config Library <../libraries/config>` documentation. |
| 117 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | index_page() |
| 119 | ============ |
| 120 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 121 | .. php:function:: index_page() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 123 | :returns: string |
| 124 | |
| 125 | Returns your site **index_page**, as specified in your config file. |
| 126 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 127 | |
| 128 | echo index_page(); |
| 129 | |
| 130 | anchor() |
| 131 | ======== |
| 132 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 133 | .. php:function:: anchor($uri = '', $title = '', $attributes = '') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 134 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 135 | :param string $uri: URI string |
| 136 | :param string $title: Anchor title |
| 137 | :param mixed $attributes: HTML attributes |
| 138 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 140 | Creates a standard HTML anchor link based on your local site URL. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | |
| 142 | The first parameter can contain any segments you wish appended to the |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 143 | URL. As with the :php:func:`site_url()` function above, segments can |
| 144 | be a string or an array. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 145 | |
| 146 | .. note:: If you are building links that are internal to your application |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 147 | do not include the base URL (http://...). This will be added |
| 148 | automatically from the information specified in your config file. |
| 149 | Include only the URI segments you wish appended to the URL. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | |
| 151 | The second segment is the text you would like the link to say. If you |
| 152 | leave it blank, the URL will be used. |
| 153 | |
| 154 | The third parameter can contain a list of attributes you would like |
| 155 | added to the link. The attributes can be a simple string or an |
| 156 | associative array. |
| 157 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 158 | Here are some examples:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 159 | |
| 160 | echo anchor('news/local/123', 'My News', 'title="News title"'); |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 161 | // Prints: <a href="http://example.com/index.php/news/local/123" title="News title">My News</a> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
| 163 | echo anchor('news/local/123', 'My News', array('title' => 'The best news!')); |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 164 | // Prints: <a href="http://example.com/index.php/news/local/123" title="The best news!">My News</a> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 165 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 166 | echo anchor('', 'Click here'); |
| 167 | // Prints: <a href="http://example.com">Click Here</a> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
| 169 | anchor_popup() |
| 170 | ============== |
| 171 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 172 | .. php:function:: anchor_popup($uri = '', $title = '', $attributes = FALSE) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 174 | :param string $uri: URI string |
| 175 | :param string $title: Anchor title |
| 176 | :param mixed $attributes: HTML attributes |
| 177 | :returns: string |
| 178 | |
| 179 | Nearly identical to the :php:func:``anchor()`` function except that it |
| 180 | opens the URL in a new window. You can specify JavaScript window |
| 181 | attributes in the third parameter to control how the window is opened. |
| 182 | If the third parameter is not set it will simply open a new window with |
| 183 | your own browser settings. |
| 184 | |
| 185 | Here is an example with attributes:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 186 | |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 187 | $atts = array( |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 188 | 'width' => 800, |
| 189 | 'height' => 600, |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 190 | 'scrollbars' => 'yes', |
| 191 | 'status' => 'yes', |
| 192 | 'resizable' => 'yes', |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 193 | 'screenx' => 0, |
| 194 | 'screeny' => 0, |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 195 | 'window_name' => '_blank' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 196 | ); |
| 197 | |
| 198 | echo anchor_popup('news/local/123', 'Click Me!', $atts); |
| 199 | |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 200 | .. note:: The above attributes are the function defaults so you only need to |
Derek Jones | ce79be0 | 2012-06-25 23:23:46 -0700 | [diff] [blame] | 201 | set the ones that are different from what you need. If you want the |
| 202 | function to use all of its defaults simply pass an empty array in the |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 203 | third parameter: |
| 204 | | |
| 205 | | echo anchor_popup('news/local/123', 'Click Me!', array()); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 206 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 207 | .. note:: The **window_name** is not really an attribute, but an argument to |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 208 | the JavaScript `window.open() <http://www.w3schools.com/jsref/met_win_open.asp>` |
| 209 | method, which accepts either a window name or a window target. |
| 210 | |
| 211 | .. note:: Any other attribute than the listed above will be parsed as an |
| 212 | HTML attribute to the anchor tag. |
| 213 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 214 | mailto() |
| 215 | ======== |
| 216 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 217 | .. php:function:: mailto($email, $title = '', $attributes = '') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 218 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 219 | :param string $email: E-mail address |
| 220 | :param string $title: Anchor title |
| 221 | :param mixed $attributes: HTML attributes |
| 222 | :returns: string |
| 223 | |
| 224 | Creates a standard HTML e-mail link. Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 225 | |
| 226 | echo mailto('me@my-site.com', 'Click Here to Contact Me'); |
| 227 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 228 | As with the :php:func:`anchor()` tab above, you can set attributes using the |
| 229 | third parameter:: |
InFog | 00b3df4 | 2012-07-29 13:42:50 -0300 | [diff] [blame] | 230 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 231 | $attributes = array('title' => 'Mail me'); |
| 232 | echo mailto('me@my-site.com', 'Contact Me', $attributes); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 233 | |
| 234 | safe_mailto() |
| 235 | ============= |
| 236 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 237 | .. php:function:: safe_mailto($email, $title = '', $attributes = '') |
| 238 | |
| 239 | :param string $email: E-mail address |
| 240 | :param string $title: Anchor title |
| 241 | :param mixed $attributes: HTML attributes |
| 242 | :returns: string |
| 243 | |
| 244 | Identical to the :php:func:`mailto()` function except it writes an obfuscated |
| 245 | version of the *mailto* tag using ordinal numbers written with JavaScript to |
| 246 | help prevent the e-mail address from being harvested by spam bots. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 247 | |
| 248 | auto_link() |
| 249 | =========== |
| 250 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 251 | .. php:function:: auto_link($str, $type = 'both', $popup = FALSE) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 252 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 253 | :param string $str: Input string |
| 254 | :param string $type: Link type ('email', 'url' or 'both') |
| 255 | :param bool $popup: Whether to create popup links |
| 256 | :returns: string |
| 257 | |
| 258 | Automatically turns URLs and e-mail addresses contained in a string into |
| 259 | links. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 260 | |
| 261 | $string = auto_link($string); |
| 262 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 263 | The second parameter determines whether URLs and e-mails are converted or |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 264 | just one or the other. Default behavior is both if the parameter is not |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 265 | specified. E-mail links are encoded as :php:func:`safe_mailto()` as shown |
| 266 | above. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 267 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 268 | Converts only URLs:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 269 | |
| 270 | $string = auto_link($string, 'url'); |
| 271 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 272 | Converts only e-mail addresses:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 273 | |
| 274 | $string = auto_link($string, 'email'); |
| 275 | |
| 276 | The third parameter determines whether links are shown in a new window. |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 277 | The value can be TRUE or FALSE (boolean):: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 278 | |
| 279 | $string = auto_link($string, 'both', TRUE); |
| 280 | |
| 281 | url_title() |
| 282 | =========== |
| 283 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 284 | .. php:function:: url_title($str, $separator = '-', $lowercase = FALSE) |
| 285 | |
| 286 | :param string $str: Input string |
| 287 | :param string $separator: Word separator |
| 288 | :param string $lowercase: Whether to transform the output string to lower-case |
| 289 | :returns: string |
| 290 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 291 | Takes a string as input and creates a human-friendly URL string. This is |
| 292 | useful if, for example, you have a blog in which you'd like to use the |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 293 | title of your entries in the URL. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 294 | |
| 295 | $title = "What's wrong with CSS?"; |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 296 | $url_title = url_title($title); |
| 297 | // Produces: Whats-wrong-with-CSS |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 298 | |
| 299 | The second parameter determines the word delimiter. By default dashes |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 300 | are used. Preferred options are: **-** (dash) or **_** (underscore) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 301 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 302 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 303 | |
| 304 | $title = "What's wrong with CSS?"; |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 305 | $url_title = url_title($title, 'underscore'); |
| 306 | // Produces: Whats_wrong_with_CSS |
| 307 | |
| 308 | .. note:: Old usage of 'dash' and 'underscore' as the second parameter |
| 309 | is DEPRECATED. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 310 | |
| 311 | The third parameter determines whether or not lowercase characters are |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 312 | forced. By default they are not. Options are boolean TRUE/FALSE. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 313 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 314 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 315 | |
| 316 | $title = "What's wrong with CSS?"; |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 317 | $url_title = url_title($title, 'underscore', TRUE); |
| 318 | // Produces: whats_wrong_with_css |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 319 | |
| 320 | prep_url() |
| 321 | ---------- |
| 322 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 323 | .. php:function:: prep_url($str = '') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 324 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 325 | :param string $str: URL string |
| 326 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 327 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 328 | This function will add http:// in the event that a protocol prefix |
| 329 | is missing from a URL. |
| 330 | |
| 331 | Pass the URL string to the function like this:: |
| 332 | |
| 333 | $url = prep_url('example.com'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 334 | |
| 335 | redirect() |
| 336 | ========== |
| 337 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 338 | .. php:function:: redirect($uri = '', $method = 'auto', $code = NULL) |
| 339 | |
| 340 | :param string $uri: URI string |
| 341 | :param string $method: Redirect method ('auto', 'location' or 'refresh') |
| 342 | :param string $code: HTTP Response code (usually 302 or 303) |
| 343 | :returns: void |
| 344 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 345 | Does a "header redirect" to the URI specified. If you specify the full |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 346 | site URL that link will be built, but for local links simply providing |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 347 | the URI segments to the controller you want to direct to will create the |
| 348 | link. The function will build the URL based on your config file values. |
| 349 | |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 350 | The optional second parameter allows you to force a particular redirection |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 351 | method. The available methods are **auto**, **location** and **refresh**, |
| 352 | with location being faster but less reliable on IIS servers. |
| 353 | The default is **auto**, which will attempt to intelligently choose the |
| 354 | method based on the server environment. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 355 | |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 356 | The optional third parameter allows you to send a specific HTTP Response |
| 357 | Code - this could be used for example to create 301 redirects for search |
| 358 | engine purposes. The default Response Code is 302. The third parameter is |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 359 | *only* available with **location** redirects, and not *refresh*. Examples:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 360 | |
| 361 | if ($logged_in == FALSE) |
| 362 | { |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 363 | redirect('/login/form/'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // with 301 redirect |
| 367 | redirect('/article/13', 'location', 301); |
| 368 | |
| 369 | .. note:: In order for this function to work it must be used before anything |
| 370 | is outputted to the browser since it utilizes server headers. |
| 371 | |
| 372 | .. note:: For very fine grained control over headers, you should use the |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 373 | `Output Library </libraries/output>` ``set_header()`` method. |
vlakoff | 530b946 | 2012-09-17 14:18:07 +0200 | [diff] [blame] | 374 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 375 | .. note:: To IIS users: if you hide the `Server` HTTP header, the *auto* |
vlakoff | 530b946 | 2012-09-17 14:18:07 +0200 | [diff] [blame] | 376 | method won't detect IIS, in that case it is advised you explicitly |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 377 | use the **refresh** method. |
| 378 | |
| 379 | .. note:: When the **location** method is used, an HTTP status code of 303 |
| 380 | will *automatically* be selected when the page is currently accessed |
| 381 | via POST and HTTP/1.1 is used. |
| 382 | |
Andrey Andreev | 1a00149 | 2013-01-24 11:32:29 +0200 | [diff] [blame] | 383 | .. important:: This function will terminate script execution. |