blob: 1987dfb72b17dc9ea13c1be8ef6d193000be3a05 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##########
2URL Helper
3##########
4
5The URL Helper file contains functions that assist in working with URLs.
6
7.. contents:: Page Contents
8
9Loading this Helper
10===================
11
12This helper is loaded using the following code
13
14::
15
16 $this->load->helper('url');
17
18The following functions are available:
19
20site_url()
21==========
22
23Returns your site URL, as specified in your config file. The index.php
24file (or whatever you have set as your site index_page in your config
25file) will be added to the URL, as will any URI segments you pass to the
26function, and the url_suffix as set in your config file.
27
28You are encouraged to use this function any time you need to generate a
29local URL so that your pages become more portable in the event your URL
30changes.
31
32Segments can be optionally passed to the function as a string or an
33array. Here is a string example
34
35::
36
37 echo site_url("news/local/123");
38
39The above example would return something like:
40http://example.com/index.php/news/local/123
41
42Here is an example of segments passed as an array
43
44::
45
46 $segments = array('news', 'local', '123');
47 echo site_url($segments);
48
49base_url()
50===========
51
52Returns your site base URL, as specified in your config file. Example
53
54::
55
56 echo base_url();
57
58This function returns the same thing as `site_url`, without the
59index_page or url_suffix being appended.
60
61Also like site_url, you can supply segments as a string or an array.
62Here is a string example
63
64::
65
66 echo base_url("blog/post/123");
67
68The above example would return something like:
69http://example.com/blog/post/123
70
71This is useful because unlike `site_url()`, you can supply a string to a
72file, such as an image or stylesheet. For example
73
74::
75
76 echo base_url("images/icons/edit.png");
77
78This would give you something like:
79http://example.com/images/icons/edit.png
80
81current_url()
82=============
83
84Returns the full URL (including segments) of the page being currently
85viewed.
86
87uri_string()
88============
89
90Returns the URI segments of any page that contains this function. For
91example, if your URL was this
92
93::
94
95 http://some-site.com/blog/comments/123
96
97The function would return
98
99::
100
101 /blog/comments/123
102
103index_page()
104============
105
106Returns your site "index" page, as specified in your config file.
107Example
108
109::
110
111 echo index_page();
112
113anchor()
114========
115
116Creates a standard HTML anchor link based on your local site URL
117
118::
119
120 <a href="http://example.com">Click Here</a>
121
122The tag has three optional parameters
123
124::
125
126 anchor(uri segments, text, attributes)
127
128The first parameter can contain any segments you wish appended to the
129URL. As with the site_url() function above, segments can be a string or
130an 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
137The second segment is the text you would like the link to say. If you
138leave it blank, the URL will be used.
139
140The third parameter can contain a list of attributes you would like
141added to the link. The attributes can be a simple string or an
142associative array.
143
144Here are some examples
145
146::
147
148 echo anchor('news/local/123', 'My News', 'title="News title"');
149
150Would produce: <a href="http://example.com/index.php/news/local/123"
151title="News title">My News</a>
152
153::
154
155 echo anchor('news/local/123', 'My News', array('title' => 'The best news!'));
156
157Would produce: <a href="http://example.com/index.php/news/local/123"
158title="The best news!">My News</a>
159
160anchor_popup()
161==============
162
163Nearly identical to the anchor() function except that it opens the URL
164in a new window. You can specify JavaScript window attributes in the
165third parameter to control how the window is opened. If the third
166parameter is not set it will simply open a new window with your own
167browser settings. Here is an example with attributes
168
169::
170
Andrey Andreev81c32082012-06-16 21:21:46 +0300171 $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 Jones8ede1a22011-10-05 13:34:52 -0500180 );
181
182 echo anchor_popup('news/local/123', 'Click Me!', $atts);
183
Andrey Andreev81c32082012-06-16 21:21:46 +0300184.. note:: The above attributes are the function defaults so you only need to
Derek Jonesce79be02012-06-25 23:23:46 -0700185 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 Jones8ede1a22011-10-05 13:34:52 -0500188
189::
190
191 echo anchor_popup('news/local/123', 'Click Me!', array());
192
Andrey Andreev81c32082012-06-16 21:21:46 +0300193.. 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 Jones8ede1a22011-10-05 13:34:52 -0500200mailto()
201========
202
203Creates a standard HTML email link. Usage example
204
205::
206
207 echo mailto('me@my-site.com', 'Click Here to Contact Me');
208
209As with the anchor() tab above, you can set attributes using the third
InFog00b3df42012-07-29 13:42:50 -0300210parameter:
211
212::
213
214 $attributes = array('title' => 'Mail me');
215 echo mailto('me@my-site.com', 'Contact Me', $attributes);
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
217safe_mailto()
218=============
219
220Identical to the above function except it writes an obfuscated version
221of the mailto tag using ordinal numbers written with JavaScript to help
222prevent the email address from being harvested by spam bots.
223
224auto_link()
225===========
226
227Automatically turns URLs and email addresses contained in a string into
228links. Example
229
230::
231
232 $string = auto_link($string);
233
234The second parameter determines whether URLs and emails are converted or
235just one or the other. Default behavior is both if the parameter is not
236specified. Email links are encoded as safe_mailto() as shown above.
237
238Converts only URLs
239
240::
241
242 $string = auto_link($string, 'url');
243
244Converts only Email addresses
245
246::
247
248 $string = auto_link($string, 'email');
249
250The third parameter determines whether links are shown in a new window.
251The value can be TRUE or FALSE (boolean)
252
253::
254
255 $string = auto_link($string, 'both', TRUE);
256
257url_title()
258===========
259
260Takes a string as input and creates a human-friendly URL string. This is
261useful if, for example, you have a blog in which you'd like to use the
262title 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
269The second parameter determines the word delimiter. By default dashes
270are 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
277The third parameter determines whether or not lowercase characters are
278forced. 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
285prep_url()
286----------
287
288This function will add http:// in the event that a scheme is missing
289from a URL. Pass the URL string to the function like this
290
291::
292
293 $url = "example.com";
294 $url = prep_url($url);
295
296redirect()
297==========
298
299Does a "header redirect" to the URI specified. If you specify the full
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500300site URL that link will be built, but for local links simply providing
Derek Jones8ede1a22011-10-05 13:34:52 -0500301the URI segments to the controller you want to direct to will create the
302link. The function will build the URL based on your config file values.
303
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500304The optional second parameter allows you to force a particular redirection
305method. The available methods are "location" or "refresh", with location
vlakoffaab26a12012-09-11 13:10:21 +0200306being faster but less reliable on IIS servers. The default is "auto",
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500307which will attempt to intelligently choose the method based on the server
308environment.
Derek Jones8ede1a22011-10-05 13:34:52 -0500309
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500310The optional third parameter allows you to send a specific HTTP Response
311Code - this could be used for example to create 301 redirects for search
312engine purposes. The default Response Code is 302. The third parameter is
313*only* available with 'location' redirects, and not 'refresh'. Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500314
315 if ($logged_in == FALSE)
316 {      
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500317 redirect('/login/form/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500318 }
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.
vlakoff530b9462012-09-17 14:18:07 +0200328
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.