blob: e6d51b22b21d660703c1f25dfb135b6e1ca11c9c [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
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 );
180
181 echo anchor_popup('news/local/123', 'Click Me!', $atts);
182
183Note: The above attributes are the function defaults so you only need to
184set the ones that are different from what you need. If you want the
185function to use all of its defaults simply pass an empty array in the
186third parameter
187
188::
189
190 echo anchor_popup('news/local/123', 'Click Me!', array());
191
192mailto()
193========
194
195Creates a standard HTML email link. Usage example
196
197::
198
199 echo mailto('me@my-site.com', 'Click Here to Contact Me');
200
201As with the anchor() tab above, you can set attributes using the third
202parameter.
203
204safe_mailto()
205=============
206
207Identical to the above function except it writes an obfuscated version
208of the mailto tag using ordinal numbers written with JavaScript to help
209prevent the email address from being harvested by spam bots.
210
211auto_link()
212===========
213
214Automatically turns URLs and email addresses contained in a string into
215links. Example
216
217::
218
219 $string = auto_link($string);
220
221The second parameter determines whether URLs and emails are converted or
222just one or the other. Default behavior is both if the parameter is not
223specified. Email links are encoded as safe_mailto() as shown above.
224
225Converts only URLs
226
227::
228
229 $string = auto_link($string, 'url');
230
231Converts only Email addresses
232
233::
234
235 $string = auto_link($string, 'email');
236
237The third parameter determines whether links are shown in a new window.
238The value can be TRUE or FALSE (boolean)
239
240::
241
242 $string = auto_link($string, 'both', TRUE);
243
244url_title()
245===========
246
247Takes a string as input and creates a human-friendly URL string. This is
248useful if, for example, you have a blog in which you'd like to use the
249title of your entries in the URL. Example
250
251::
252
253 $title = "What's wrong with CSS?";
254 $url_title = url_title($title); // Produces: Whats-wrong-with-CSS
255
256The second parameter determines the word delimiter. By default dashes
257are used. Options are: dash, or underscore
258
259::
260
261 $title = "What's wrong with CSS?";
262 $url_title = url_title($title, 'underscore'); // Produces: Whats_wrong_with_CSS
263
264The third parameter determines whether or not lowercase characters are
265forced. By default they are not. Options are boolean TRUE/FALSE
266
267::
268
269 $title = "What's wrong with CSS?";
270 $url_title = url_title($title, 'underscore', TRUE); // Produces: whats_wrong_with_css
271
272prep_url()
273----------
274
275This function will add http:// in the event that a scheme is missing
276from a URL. Pass the URL string to the function like this
277
278::
279
280 $url = "example.com";
281 $url = prep_url($url);
282
283redirect()
284==========
285
286Does a "header redirect" to the URI specified. If you specify the full
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500287site URL that link will be built, but for local links simply providing
Derek Jones8ede1a22011-10-05 13:34:52 -0500288the URI segments to the controller you want to direct to will create the
289link. The function will build the URL based on your config file values.
290
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500291The optional second parameter allows you to force a particular redirection
292method. The available methods are "location" or "refresh", with location
293being faster but less reliable on Windows servers. The default is "auto",
294which will attempt to intelligently choose the method based on the server
295environment.
Derek Jones8ede1a22011-10-05 13:34:52 -0500296
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500297The optional third parameter allows you to send a specific HTTP Response
298Code - this could be used for example to create 301 redirects for search
299engine purposes. The default Response Code is 302. The third parameter is
300*only* available with 'location' redirects, and not 'refresh'. Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500301
302 if ($logged_in == FALSE)
303 {      
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500304 redirect('/login/form/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500305 }
306
307 // with 301 redirect
308 redirect('/article/13', 'location', 301);
309
310.. note:: In order for this function to work it must be used before anything
311 is outputted to the browser since it utilizes server headers.
312
313.. note:: For very fine grained control over headers, you should use the
314 `Output Library </libraries/output>` set_header() function.