blob: f04bb9f10e023d70bd9641a9d9924d8a81337806 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<title>URI Class : CodeIgniter User Guide</title>
7
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11<script type="text/javascript" src="../nav/nav.js"></script>
12<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13<script type="text/javascript" src="../nav/moo.fx.js"></script>
14<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
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='ExpressionEngine Dev Team' />
20<meta name='description' content='CodeIgniter User Guide' />
21
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_darker.jpg" width="154" height="43" 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>
Phil Sturgeon9c63d0b2011-10-27 01:55:44 +010031<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33</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">
43<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45URI Class
46</td>
47<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>
48</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
61<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>
63
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
68<p>Permits you to retrieve a specific segment. Where <var>n</var> is the segment number you wish to retrieve.
69Segments are numbered from left to right. For example, if your full URL is this:</p>
70
71<code>http://example.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
82<p>By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that
83permits 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
100<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
103re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
104
105
106<h2>$this->uri->slash_segment(<var>n</var>)</h2>
107
108<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
Derek Jones4b9c6292011-07-01 17:40:48 -0500109parameter. If the parameter is not used, a trailing slash added. Examples:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000110
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
124<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
127re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
128
129
130
131<h2>$this->uri->uri_to_assoc(<var>n</var>)</h2>
132
Derek Jones4b9c6292011-07-01 17:40:48 -0500133<p>This function lets you turn URI segments into and associative array of key/value pairs. Consider this URI:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000134
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
Derek Jones4b9c6292011-07-01 17:40:48 -0500146<p>The first parameter of the function lets you set an offset. By default it is set to <kbd>3</kbd> since your
Derek Allard2067d1a2008-11-13 22:59:24 +0000147URI 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
168<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
171re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
172
173
174<h2>$this->uri->assoc_to_uri()</h2>
175
Derek Jones4b9c6292011-07-01 17:40:48 -0500176<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>
Derek Allard2067d1a2008-11-13 22:59:24 +0000177
178<code>$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');<br />
179<br />
180$str = $this->uri->assoc_to_uri($array);<br />
181<br />
Derek Jones4b9c6292011-07-01 17:40:48 -0500182// Produces: product/shoes/size/large/color/red
Derek Allard2067d1a2008-11-13 22:59:24 +0000183</code>
184
185
186<h2>$this->uri->uri_string()</h2>
187
Derek Jones4b9c6292011-07-01 17:40:48 -0500188<p>Returns a string with the complete URI. For example, if this is your full URL:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000189
190<code>http://example.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
Derek Allardb5f211b2010-01-17 15:42:56 +0000197<h2>$this->uri->ruri_string()</h2>
Derek Allard2067d1a2008-11-13 22:59:24 +0000198
199<p>This function is identical to the previous one, except that it returns the
200re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
201
202
203
204<h2>$this->uri->total_segments()</h2>
205
206<p>Returns the total number of segments.</p>
207
208
209<h2>$this->uri->total_rsegments()</h2>
210
211<p>This function is identical to the previous one, except that it returns the total number of segments in your
212re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
213
214
215
216<h2>$this->uri->segment_array()</h2>
217
Derek Jones4b9c6292011-07-01 17:40:48 -0500218<p>Returns an array containing the URI segments. For example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000219
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
229<h2>$this->uri->rsegment_array()</h2>
230
231<p>This function is identical to the previous one, except that it returns the array of segments in your
232re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
233
234
235
236</div>
237<!-- END CONTENT -->
238
239
240<div id="footer">
241<p>
242Previous Topic:&nbsp;&nbsp;<a href="unit_testing.html">Unit Testing Class</a>
243&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;
246Next Topic:&nbsp;&nbsp;<a href="user_agent.html">User Agent Class</a>
247</p>
Derek Jones898949f2011-01-28 07:42:16 -0600248<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000249</div>
250
251</body>
Derek Allard39b622d2008-01-16 21:10:09 +0000252</html>