blob: 7efca6dd135ace8476350ee8c18294813521fc3d [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
Derek Allardd2df9bc2007-04-15 17:41:17 +00005<title>CodeIgniter User Guide</title>
adminb0dd10f2006-08-25 17:25:49 +00006
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() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminb0dd10f2006-08-25 17:25:49 +000016 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' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000026
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>
Derek Allardd2df9bc2007-04-15 17:41:17 +000036<td><h1>CodeIgniter User Guide Version 1.5.3</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000038</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">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50URI Class
51</td>
Derek Allardbc030912007-06-24 18:25:29 +000052<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="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>
adminb0dd10f2006-08-25 17:25:49 +000053</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
64<h1>URI Class</h1>
65
admin05633d72006-09-21 17:22:21 +000066<p>The URI Class provides functions that help you retrieve information from your URI strings. If you use URI routing, you can
67also retrieve information about the re-routed segments.</p>
adminb0dd10f2006-08-25 17:25:49 +000068
69<p class="important"><strong>Note:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
70
71<h2>$this->uri->segment(<var>n</var>)</h2>
72
admine334c472006-10-21 19:44:22 +000073<p>Permits you to retrieve a specific segment. Where <var>n</var> is the segment number you wish to retrieve.
adminb0dd10f2006-08-25 17:25:49 +000074Segments are numbered from left to right. For example, if your full URL is this:</p>
75
76<code>http://www.your-site.com/index.php/news/local/metro/crime_is_up</code>
77
78<p>The segment numbers would be this:</p>
79
80<ol>
81<li>news</li>
82<li>local</li>
83<li>metro</li>
84<li>crime_is_up</li>
85</ol>
86
admine334c472006-10-21 19:44:22 +000087<p>By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that
adminb0dd10f2006-08-25 17:25:49 +000088permits you to set your own default value if the segment is missing.
89For example, this would tell the function to return the number zero in the event of failure:</p>
90
91<code>$product_id = $this->uri->segment(3, 0);</code>
92
93<p>It helps avoid having to write code like this:</p>
94
95<code>if ($this->uri->segment(3) === FALSE)<br />
96{<br />
97&nbsp;&nbsp;&nbsp;&nbsp;$product_id = 0;<br />
98}<br />
99else<br />
100{<br />
101&nbsp;&nbsp;&nbsp;&nbsp;$product_id = $this->uri->segment(3);<br />
102}<br />
103</code>
104
admin05633d72006-09-21 17:22:21 +0000105<h2>$this->uri->rsegment(<var>n</var>)</h2>
106
107<p>This function is identical to the previous one, except that it lets you retrieve a specific segment from your
Derek Allardc6441282007-07-04 23:54:32 +0000108re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
admin05633d72006-09-21 17:22:21 +0000109
adminb0dd10f2006-08-25 17:25:49 +0000110
111<h2>$this->uri->slash_segment(<var>n</var>)</h2>
112
admin05633d72006-09-21 17:22:21 +0000113<p>This function is almost identical to <dfn>$this->uri->segment()</dfn>, except it adds a trailing and/or leading slash based on the second
adminb0dd10f2006-08-25 17:25:49 +0000114parameter. If the parameter is not used, a trailing slash added. Examples:</p>
115
116<code>$this->uri->slash_segment(<var>3</var>);<br />
117$this->uri->slash_segment(<var>3</var>, 'leading');<br />
118$this->uri->slash_segment(<var>3</var>, 'both');</code>
119
120<p>Returns:</p>
121
122<ol>
123<li>segment/</li>
124<li>/segment</li>
125<li>/segment/</li>
126</ol>
127
128
admin05633d72006-09-21 17:22:21 +0000129<h2>$this->uri->slash_rsegment(<var>n</var>)</h2>
130
131<p>This function is identical to the previous one, except that it lets you add slashes a specific segment from your
Derek Allardc6441282007-07-04 23:54:32 +0000132re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
admin05633d72006-09-21 17:22:21 +0000133
134
135
adminb0dd10f2006-08-25 17:25:49 +0000136<h2>$this->uri->uri_to_assoc(<var>n</var>)</h2>
137
138<p>This function lets you turn URI segments into and associative array of key/value pairs. Consider this URI:</p>
139
140<code>index.php/user/search/name/joe/location/UK/gender/male</code>
141
142<p>Using this function you can turn the URI into an associative array with this prototype:</p>
143
144<code>[array]<br />
145(<br />
146&nbsp;&nbsp;&nbsp;&nbsp;'name' => 'joe'<br />
147&nbsp;&nbsp;&nbsp;&nbsp;'location' => 'UK'<br />
148&nbsp;&nbsp;&nbsp;&nbsp;'gender' => 'male'<br />
149)</code>
150
151<p>The first parameter of the function lets you set an offset. By default it is set to <kbd>3</kbd> since your
152URI will normally contain a controller/function in the first and second segments. Example:</p>
153
154<code>
155$array = $this->uri->uri_to_assoc(3);<br />
156<br />
157echo $array['name'];
158</code>
159
160
161<p>The second parameter lets you set default key names, so that the array returned by the function will always contain expected indexes, even if missing from the URI. Example:</p>
162
163<code>
164$default = array('name', 'gender', 'location', 'type', 'sort');<br />
165<br />
166$array = $this->uri->uri_to_assoc(3, $default);</code>
167
168<p>If the URI does not contain a value in your default, an array index will be set to that name, with a value of FALSE.</p>
169
170<p>Lastly, if a corresponding value is not found for a given key (if there is an odd number of URI segments) the value will be set to FALSE (boolean).</p>
171
172
admin05633d72006-09-21 17:22:21 +0000173<h2>$this->uri->ruri_to_assoc(<var>n</var>)</h2>
174
175<p>This function is identical to the previous one, except that it creates an associative array using the
Derek Allardc6441282007-07-04 23:54:32 +0000176re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
admin05633d72006-09-21 17:22:21 +0000177
178
adminb0dd10f2006-08-25 17:25:49 +0000179<h2>$this->uri->assoc_to_uri()</h2>
180
181<p>Takes an associative array as input and generates a URI string from it. The array keys will be included in the string. Example:</p>
182
183<code>$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');<br />
184<br />
Derek Allardc6409942007-01-27 14:23:27 +0000185$str = $this->uri->assoc_to_uri($array);<br />
adminb0dd10f2006-08-25 17:25:49 +0000186<br />
187// Produces: product/shoes/size/large/color/red
188</code>
189
190
191<h2>$this->uri->uri_string()</h2>
192
193<p>Returns a string with the complete URI. For example, if this is your full URL:</p>
194
195<code>http://www.your-site.com/index.php/news/local/345</code>
196
197<p>The function would return this:</p>
198
199<code>news/local/345</code>
200
201
admin05633d72006-09-21 17:22:21 +0000202<h2>$this->uri->ruri_string(<var>n</var>)</h2>
203
204<p>This function is identical to the previous one, except that it returns the
Derek Allardc6441282007-07-04 23:54:32 +0000205re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
admin05633d72006-09-21 17:22:21 +0000206
207
208
adminb0dd10f2006-08-25 17:25:49 +0000209<h2>$this->uri->total_segments()</h2>
210
211<p>Returns the total number of segments.</p>
212
213
admin05633d72006-09-21 17:22:21 +0000214<h2>$this->uri->total_rsegments(<var>n</var>)</h2>
215
216<p>This function is identical to the previous one, except that it returns the total number of segments in your
Derek Allardc6441282007-07-04 23:54:32 +0000217re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
admin05633d72006-09-21 17:22:21 +0000218
219
adminb0dd10f2006-08-25 17:25:49 +0000220
221<h2>$this->uri->segment_array()</h2>
222
223<p>Returns an array containing the URI segments. For example:</p>
224
225<code>
226$segs = $this->uri->segment_array();<br />
227<br />
228foreach ($segs as $segment)<br />
229{<br />
230&nbsp;&nbsp;&nbsp;&nbsp;echo $segment;<br />
231&nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;br />';<br />
232}</code>
233
admin05633d72006-09-21 17:22:21 +0000234<h2>$this->uri->rsegment_array(<var>n</var>)</h2>
235
236<p>This function is identical to the previous one, except that it returns the array of segments in your
Derek Allardd2df9bc2007-04-15 17:41:17 +0000237re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.
admin05633d72006-09-21 17:22:21 +0000238
adminb0dd10f2006-08-25 17:25:49 +0000239
240
241</div>
242<!-- END CONTENT -->
243
244
245<div id="footer">
246<p>
Derek Allard9da4dbc2007-04-03 11:39:35 +0000247Previous Topic:&nbsp;&nbsp;<a href="unit_testing.html">Unit Testing Class</a>
adminb0dd10f2006-08-25 17:25:49 +0000248&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
249<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
250<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin6c9f7362006-10-09 03:33:48 +0000251Next Topic:&nbsp;&nbsp;<a href="user_agent.html">User Agent Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000252</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000253<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000254</div>
255
256</body>
257</html>