blob: 49855951e9b2c3a6a9ad6ec70ef69674401a9429 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
5<title>Code Igniter User Guide</title>
6
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000013<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
25<meta name='description' content='Code Igniter User Guide' />
26
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
admin41a16852006-09-26 18:17:19 +000036<td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
adminb0dd10f2006-08-25 17:25:49 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50URL Helper
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
64<h1>URL Helper</h1>
65
66<p>The URL Helper file contains functions that assist in working with URLs.</p>
67
68
69<h2>Loading this Helper</h2>
70
71<p>This helper is loaded using the following code:</p>
72<code>$this->load->helper('url');</code>
73
74<p>The following functions are available:</p>
75
76<h2>site_url()</h2>
77
78<p>Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your
79site <dfn>index_page</dfn> in your config file) will be added to the URL, as will any URI segments you pass to the function.</p>
80
81<p>You are encouraged to use this function any time you need to generate a local URL so that your pages become more portable
82in the event your URL changes.</p>
83
84<p>Segments can be optionally passed to the function as a string or an array. Here is a string example:</p>
85
86<code>echo site_url("news/local/123");</code>
87
88<p>The above example would return something like: http://www.your-site.com/index.php/news/local/123</p>
89
90<p>Here is an example of segments passed as an array:</p>
91
92<code>
93$segments = array('news', 'local', '123');<br />
94<br />
95echo site_url($segments);</code>
96
97
98<h2>base_url()</h2>
99<p>Returns your site base URL, as specified in your config file. Example:</p>
100<code>echo base_url();</code>
101
102
103<h2>index_page()</h2>
104<p>Returns your site "index" page, as specified in your config file. Example:</p>
105<code>echo index_page();</code>
106
107
108
109<h2>anchor()</h2>
110
111<p>Creates a standard HTML anchor link based on your local site URL:</p>
112
113<code>&lt;a href="http://www.your-site.com">Click Here&lt;/a></code>
114
115<p>The tag has three optional parameters:</p>
116
117<code>anchor(<var>uri segments</var>, <var>text</var>, <var>attributes</var>)</code>
118
119<p>The first parameter can contain any segments you wish appended to the URL. As with the <dfn>site_url()</dfn> function above,
adminb071bb52006-08-26 19:28:37 +0000120segments can be a string or an array.</p>
121
122<p><strong>Note:</strong>&nbsp; If you are building links that are internal to your application do not include the base URL (http://...). This
123will be added automatically from the information specified in your config file. Include only the URI segments you wish appended to the URL.</p>
adminb0dd10f2006-08-25 17:25:49 +0000124
125<p>The second segment is the text you would like the link to say. If you leave it blank, the URL will be used.</p>
126
127<p>The third parameter can contain a list of attributes you would like added to the link. The attributes can be a simple string or an associative array.</p>
128
129<p>Here are some examples:</p>
130
131<code>echo anchor(<var>news/local/123</var>, <var>My News</var>);</code>
132
133<p>Would produce: &lt;a href="http://www.your-site.com/index.php/news/local/123" title="My News">My News&lt;/a></p>
134
135<code>echo anchor(<var>news/local/123</var>, <var>My News</var>, <var>array('title' => 'The best news!')</var>);</code>
136
137<p>Would produce: &lt;a href="http://www.your-site.com/index.php/news/local/123" title="The best news!">My News&lt;/a></p>
138
139
140<h2>anchor_popup()</h2>
141
142<p>Nearly identical to the <dfn>anchor()</dfn> function except that it opens the URL in a new window.
143
144You can specify JavaScript window attributes in the third parameter to control how the window is opened. If
145the third parameter is not set it will simply open a new window with your own browser settings. Here is an example
146with attributes:</p>
147
148<code>
149
150$atts = array(<br />
151&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'width'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '800',<br />
152&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'height'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '600',<br />
153&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'scrollbars' => 'yes',<br />
154&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'status'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 'yes',<br />
155&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'resizable'&nbsp;&nbsp;=> 'yes',<br />
156&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'screenx'&nbsp;&nbsp;&nbsp;&nbsp;=> '0',<br />
157&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'screeny'&nbsp;&nbsp;&nbsp;&nbsp;=> '0'<br />
158&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
159<br />
160echo anchor_popup(news/local/123, 'Click Me!', $atts);</code>
161
162<p>Note: The above attributes are the function defaults so you only need to set the ones that are different from what you need.
163If you want the function to use all of its defaults simply pass an empty array in the third parameter:</p>
164
165<code>echo anchor_popup(news/local/123, 'Click Me!', array());</code>
166
167
168<h2>mailto()</h2>
169
170<p>Creates a standard HTML email link. Usage example:</p>
171
172<code>echo mailto('me@my-site.com', 'Click Here to Contact Me');</code>
173
174<p>As with the <dfn>anchor()</dfn> tab above, you can set attributes using the third parameter.</p>
175
176
177<h2>safe_mailto()</h2>
178
179<p>Identical to the above function except it writes an obfuscated version of the mailto tag using ordinal numbers
180written with JavaScript to help prevent the email address from being harvested by spam bots.</p>
181
182
183<h2>auto_link()</h2>
184
185<p>Automatically turns URLs and email addresses contained in a string into links. Example:</p>
186
187<code>$string = auto_link($string);</code>
188
189<p>The second parameter determines whether URLs and emails are converted or just one or the other. Default behavior is both
190if the parameter is not specified</p>
191
192<p>Converts only URLs:</p>
193<code>$string = auto_link($string, 'url');</code>
194
195<p>Converts only Email addresses:</p>
196<code>$string = auto_link($string, 'email');</code>
197
198</p>The third parameter determines whether links are shown in a new window. The value can be TRUE or FALSE (boolean):</p>
199<code>$string = auto_link($string, 'both', TRUE);</code>
200
201
202<h2>url_title()</h2>
203<p>Takes a string as input and creates a human-friendly URL string. This is useful if, for example, you have a blog
204in which you'd like to use the title of your entries in the URL. Example:</p>
205
206<code>$title = "What's wrong with CSS?";<br />
207<br />
208$url_title = url_title($title);<br />
209<br />
210// Produces: whats-wrong-with-css
211</code>
212
213
214<p>The second parameter determines the word delimiter. By default dashes are used. Options are: <dfn>dash</dfn>, or <dfn>underscore</dfn>:</p>
215
216<code>$title = "What's wrong with CSS?";<br />
217<br />
218$url_title = url_title($title, 'underscore');<br />
219<br />
220// Produces: whats_wrong_with_css
221</code>
222
223
224<h3>prep_url()</h3>
225<p>This function will add <kbd>http://</kbd> in the event it is missing from a URL. Pass the URL string to the function like this:</p>
226<code>
227$url = "www.some-site.com";<br /><br />
228$url = prep_url($url);</code>
229
230
231
232
233<h2>redirect()</h2>
234
235<p>Does a "header redirect" to the local URI specified. Just like other functions in this helper, this one is designed
236to redirect to a local URL within your site. You will <strong>not</strong> specify the full site URL, but rather simply the URI segments
237to the controller you want to direct to. The function will build the URL based on your config file values.</p>
238
239<p>The second parameter allows you to choose between the "location"
240method or the "refresh" method. Location is faster, but on Windows servers it can sometimes be a problem. Example:</p>
241
242<code>if ($logged_in == FALSE)<br />
243{<br />
244&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect('/login/form/', 'refresh');<br />
245}</code>
246
247<p class="important"><strong>Note:</strong> In order for this function to work it must be used before anything is outputted
248to the browser since it utilizes server headers.</p>
249
250
251
252
253
254
255</div>
256<!-- END CONTENT -->
257
258
259<div id="footer">
260<p>
261Previous Topic:&nbsp;&nbsp;<a href="typography_helper.html">Typography Helper</a>
262&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
263<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
264<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
265Next Topic:&nbsp;&nbsp;<a href="xml_helper.html">XML Helper</a>
266<p>
267<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
268</div>
269
270</body>
271</html>