blob: e221f4c6346336e6f238ab835e66f441677c3ae6 [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>
admin10c3f412006-10-08 07:21:12 +000036<td><h1>Code Igniter User Guide Version 1.5.0b1</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;
50URI Class
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>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
73<p>Permits you to retrieve a specific segment. Where <var>n</var> is the segment number you wish to retrieve.
74Segments 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
87<p>By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that
88permits 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
108re-routed URI in the event you are using Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
109
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
132re-routed URI in the event you are using Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
133
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
176re-routed URI in the event you are using Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
177
178
179
adminb0dd10f2006-08-25 17:25:49 +0000180<h2>$this->uri->assoc_to_uri()</h2>
181
182<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>
183
184<code>$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');<br />
185<br />
186$str = $this->uri->assoc_to_str($array);<br />
187<br />
188// Produces: product/shoes/size/large/color/red
189</code>
190
191
192<h2>$this->uri->uri_string()</h2>
193
194<p>Returns a string with the complete URI. For example, if this is your full URL:</p>
195
196<code>http://www.your-site.com/index.php/news/local/345</code>
197
198<p>The function would return this:</p>
199
200<code>news/local/345</code>
201
202
admin05633d72006-09-21 17:22:21 +0000203<h2>$this->uri->ruri_string(<var>n</var>)</h2>
204
205<p>This function is identical to the previous one, except that it returns the
206re-routed URI in the event you are using Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
207
208
209
adminb0dd10f2006-08-25 17:25:49 +0000210<h2>$this->uri->total_segments()</h2>
211
212<p>Returns the total number of segments.</p>
213
214
admin05633d72006-09-21 17:22:21 +0000215<h2>$this->uri->total_rsegments(<var>n</var>)</h2>
216
217<p>This function is identical to the previous one, except that it returns the total number of segments in your
218re-routed URI in the event you are using Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
219
220
adminb0dd10f2006-08-25 17:25:49 +0000221
222<h2>$this->uri->segment_array()</h2>
223
224<p>Returns an array containing the URI segments. For example:</p>
225
226<code>
227$segs = $this->uri->segment_array();<br />
228<br />
229foreach ($segs as $segment)<br />
230{<br />
231&nbsp;&nbsp;&nbsp;&nbsp;echo $segment;<br />
232&nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;br />';<br />
233}</code>
234
admin05633d72006-09-21 17:22:21 +0000235<h2>$this->uri->rsegment_array(<var>n</var>)</h2>
236
237<p>This function is identical to the previous one, except that it returns the array of segments in your
238re-routed URI in the event you are using Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
239
adminb0dd10f2006-08-25 17:25:49 +0000240
241
242</div>
243<!-- END CONTENT -->
244
245
246<div id="footer">
247<p>
248Previous Topic:&nbsp;&nbsp;<a href="parser.html">Template Parser Class</a>
249&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
250<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
251<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin6c9f7362006-10-09 03:33:48 +0000252Next Topic:&nbsp;&nbsp;<a href="user_agent.html">User Agent Class</a>
adminb0dd10f2006-08-25 17:25:49 +0000253<p>
254<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>
255</div>
256
257</body>
258</html>