blob: 0205d97e75f2918106de8f228b9d9f481b75f60e [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 Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : URI Class</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>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<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>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Allard60ca9b72007-07-12 19:53:27 +000031<td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000033</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000043<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45URI Class
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<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 +000048</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
59<h1>URI Class</h1>
60
admin05633d72006-09-21 17:22:21 +000061<p>The URI Class provides functions that help you retrieve information from your URI strings. If you use URI routing, you can
62also retrieve information about the re-routed segments.</p>
adminb0dd10f2006-08-25 17:25:49 +000063
64<p class="important"><strong>Note:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
65
66<h2>$this->uri->segment(<var>n</var>)</h2>
67
admine334c472006-10-21 19:44:22 +000068<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 +000069Segments are numbered from left to right. For example, if your full URL is this:</p>
70
71<code>http://www.your-site.com/index.php/news/local/metro/crime_is_up</code>
72
73<p>The segment numbers would be this:</p>
74
75<ol>
76<li>news</li>
77<li>local</li>
78<li>metro</li>
79<li>crime_is_up</li>
80</ol>
81
admine334c472006-10-21 19:44:22 +000082<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 +000083permits you to set your own default value if the segment is missing.
84For example, this would tell the function to return the number zero in the event of failure:</p>
85
86<code>$product_id = $this->uri->segment(3, 0);</code>
87
88<p>It helps avoid having to write code like this:</p>
89
90<code>if ($this->uri->segment(3) === FALSE)<br />
91{<br />
92&nbsp;&nbsp;&nbsp;&nbsp;$product_id = 0;<br />
93}<br />
94else<br />
95{<br />
96&nbsp;&nbsp;&nbsp;&nbsp;$product_id = $this->uri->segment(3);<br />
97}<br />
98</code>
99
admin05633d72006-09-21 17:22:21 +0000100<h2>$this->uri->rsegment(<var>n</var>)</h2>
101
102<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 +0000103re-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 +0000104
adminb0dd10f2006-08-25 17:25:49 +0000105
106<h2>$this->uri->slash_segment(<var>n</var>)</h2>
107
admin05633d72006-09-21 17:22:21 +0000108<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 +0000109parameter. If the parameter is not used, a trailing slash added. Examples:</p>
110
111<code>$this->uri->slash_segment(<var>3</var>);<br />
112$this->uri->slash_segment(<var>3</var>, 'leading');<br />
113$this->uri->slash_segment(<var>3</var>, 'both');</code>
114
115<p>Returns:</p>
116
117<ol>
118<li>segment/</li>
119<li>/segment</li>
120<li>/segment/</li>
121</ol>
122
123
admin05633d72006-09-21 17:22:21 +0000124<h2>$this->uri->slash_rsegment(<var>n</var>)</h2>
125
126<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 +0000127re-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 +0000128
129
130
adminb0dd10f2006-08-25 17:25:49 +0000131<h2>$this->uri->uri_to_assoc(<var>n</var>)</h2>
132
133<p>This function lets you turn URI segments into and associative array of key/value pairs. Consider this URI:</p>
134
135<code>index.php/user/search/name/joe/location/UK/gender/male</code>
136
137<p>Using this function you can turn the URI into an associative array with this prototype:</p>
138
139<code>[array]<br />
140(<br />
141&nbsp;&nbsp;&nbsp;&nbsp;'name' => 'joe'<br />
142&nbsp;&nbsp;&nbsp;&nbsp;'location' => 'UK'<br />
143&nbsp;&nbsp;&nbsp;&nbsp;'gender' => 'male'<br />
144)</code>
145
146<p>The first parameter of the function lets you set an offset. By default it is set to <kbd>3</kbd> since your
147URI will normally contain a controller/function in the first and second segments. Example:</p>
148
149<code>
150$array = $this->uri->uri_to_assoc(3);<br />
151<br />
152echo $array['name'];
153</code>
154
155
156<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>
157
158<code>
159$default = array('name', 'gender', 'location', 'type', 'sort');<br />
160<br />
161$array = $this->uri->uri_to_assoc(3, $default);</code>
162
163<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>
164
165<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>
166
167
admin05633d72006-09-21 17:22:21 +0000168<h2>$this->uri->ruri_to_assoc(<var>n</var>)</h2>
169
170<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 +0000171re-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 +0000172
173
adminb0dd10f2006-08-25 17:25:49 +0000174<h2>$this->uri->assoc_to_uri()</h2>
175
176<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>
177
178<code>$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');<br />
179<br />
Derek Allardc6409942007-01-27 14:23:27 +0000180$str = $this->uri->assoc_to_uri($array);<br />
adminb0dd10f2006-08-25 17:25:49 +0000181<br />
182// Produces: product/shoes/size/large/color/red
183</code>
184
185
186<h2>$this->uri->uri_string()</h2>
187
188<p>Returns a string with the complete URI. For example, if this is your full URL:</p>
189
190<code>http://www.your-site.com/index.php/news/local/345</code>
191
192<p>The function would return this:</p>
193
194<code>news/local/345</code>
195
196
admin05633d72006-09-21 17:22:21 +0000197<h2>$this->uri->ruri_string(<var>n</var>)</h2>
198
199<p>This function is identical to the previous one, except that it returns the
Derek Allardc6441282007-07-04 23:54:32 +0000200re-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 +0000201
202
203
adminb0dd10f2006-08-25 17:25:49 +0000204<h2>$this->uri->total_segments()</h2>
205
206<p>Returns the total number of segments.</p>
207
208
admin05633d72006-09-21 17:22:21 +0000209<h2>$this->uri->total_rsegments(<var>n</var>)</h2>
210
211<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 +0000212re-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 +0000213
214
adminb0dd10f2006-08-25 17:25:49 +0000215
216<h2>$this->uri->segment_array()</h2>
217
218<p>Returns an array containing the URI segments. For example:</p>
219
220<code>
221$segs = $this->uri->segment_array();<br />
222<br />
223foreach ($segs as $segment)<br />
224{<br />
225&nbsp;&nbsp;&nbsp;&nbsp;echo $segment;<br />
226&nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;br />';<br />
227}</code>
228
admin05633d72006-09-21 17:22:21 +0000229<h2>$this->uri->rsegment_array(<var>n</var>)</h2>
230
231<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 +0000232re-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 +0000233
adminb0dd10f2006-08-25 17:25:49 +0000234
235
236</div>
237<!-- END CONTENT -->
238
239
240<div id="footer">
241<p>
Derek Allard9da4dbc2007-04-03 11:39:35 +0000242Previous Topic:&nbsp;&nbsp;<a href="unit_testing.html">Unit Testing Class</a>
adminb0dd10f2006-08-25 17:25:49 +0000243&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
244<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
245<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin6c9f7362006-10-09 03:33:48 +0000246Next Topic:&nbsp;&nbsp;<a href="user_agent.html">User Agent Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000247</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000248<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 +0000249</div>
250
251</body>
252</html>