blob: 82db6a5b30dc11ec2c3b7960c531a218a2b974ec [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
210parameter.
211
212safe_mailto()
213=============
214
215Identical to the above function except it writes an obfuscated version
216of the mailto tag using ordinal numbers written with JavaScript to help
217prevent the email address from being harvested by spam bots.
218
219auto_link()
220===========
221
222Automatically turns URLs and email addresses contained in a string into
223links. Example
224
225::
226
227 $string = auto_link($string);
228
229The second parameter determines whether URLs and emails are converted or
230just one or the other. Default behavior is both if the parameter is not
231specified. Email links are encoded as safe_mailto() as shown above.
232
233Converts only URLs
234
235::
236
237 $string = auto_link($string, 'url');
238
239Converts only Email addresses
240
241::
242
243 $string = auto_link($string, 'email');
244
245The third parameter determines whether links are shown in a new window.
246The value can be TRUE or FALSE (boolean)
247
248::
249
250 $string = auto_link($string, 'both', TRUE);
251
252url_title()
253===========
254
255Takes a string as input and creates a human-friendly URL string. This is
256useful if, for example, you have a blog in which you'd like to use the
257title of your entries in the URL. Example
258
259::
260
261 $title = "What's wrong with CSS?";
262 $url_title = url_title($title); // Produces: Whats-wrong-with-CSS
263
264The second parameter determines the word delimiter. By default dashes
265are used. Options are: dash, or underscore
266
267::
268
269 $title = "What's wrong with CSS?";
270 $url_title = url_title($title, 'underscore'); // Produces: Whats_wrong_with_CSS
271
272The third parameter determines whether or not lowercase characters are
273forced. By default they are not. Options are boolean TRUE/FALSE
274
275::
276
277 $title = "What's wrong with CSS?";
278 $url_title = url_title($title, 'underscore', TRUE); // Produces: whats_wrong_with_css
279
280prep_url()
281----------
282
283This function will add http:// in the event that a scheme is missing
284from a URL. Pass the URL string to the function like this
285
286::
287
288 $url = "example.com";
289 $url = prep_url($url);
290
291redirect()
292==========
293
294Does a "header redirect" to the URI specified. If you specify the full
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500295site URL that link will be built, but for local links simply providing
Derek Jones8ede1a22011-10-05 13:34:52 -0500296the URI segments to the controller you want to direct to will create the
297link. The function will build the URL based on your config file values.
298
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500299The optional second parameter allows you to force a particular redirection
300method. The available methods are "location" or "refresh", with location
301being faster but less reliable on Windows servers. The default is "auto",
302which will attempt to intelligently choose the method based on the server
303environment.
Derek Jones8ede1a22011-10-05 13:34:52 -0500304
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500305The optional third parameter allows you to send a specific HTTP Response
306Code - this could be used for example to create 301 redirects for search
307engine purposes. The default Response Code is 302. The third parameter is
308*only* available with 'location' redirects, and not 'refresh'. Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500309
310 if ($logged_in == FALSE)
311 {      
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500312 redirect('/login/form/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500313 }
314
315 // with 301 redirect
316 redirect('/article/13', 'location', 301);
317
318.. note:: In order for this function to work it must be used before anything
319 is outputted to the browser since it utilizes server headers.
320
321.. note:: For very fine grained control over headers, you should use the
322 `Output Library </libraries/output>` set_header() function.