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