1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>URI Class : CodeIgniter User Guide</title>
<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
<script type="text/javascript" src="../nav/nav.js"></script>
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
<script type="text/javascript" src="../nav/moo.fx.js"></script>
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='ExpressionEngine Dev Team' />
<meta name='description' content='CodeIgniter User Guide' />
</head>
<body>
<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
<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>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>CodeIgniter User Guide Version Location</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->
<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://codeigniter.com/">CodeIgniter Home</a> ›
<a href="../index.html">User Guide Home</a> ›
URI Class
</td>
<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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
</tr>
</table>
<!-- END BREADCRUMB -->
<br clear="all" />
<!-- START CONTENT -->
<div id="content">
<h1>URI Class</h1>
<p>The URI Class provides functions that help you retrieve information from your URI strings. If you use URI routing, you can
also retrieve information about the re-routed segments.</p>
<p class="important"><strong>Note:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
<h2>$this->uri->segment(<var>n</var>)</h2>
<p>Permits you to retrieve a specific segment. Where <var>n</var> is the segment number you wish to retrieve.
Segments are numbered from left to right. For example, if your full URL is this:</p>
<code>http://example.com/index.php/news/local/metro/crime_is_up</code>
<p>The segment numbers would be this:</p>
<ol>
<li>news</li>
<li>local</li>
<li>metro</li>
<li>crime_is_up</li>
</ol>
<p>By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that
permits you to set your own default value if the segment is missing.
For example, this would tell the function to return the number zero in the event of failure:</p>
<code>$product_id = $this->uri->segment(3, 0);</code>
<p>It helps avoid having to write code like this:</p>
<code>if ($this->uri->segment(3) === FALSE)<br />
{<br />
$product_id = 0;<br />
}<br />
else<br />
{<br />
$product_id = $this->uri->segment(3);<br />
}<br />
</code>
<h2>$this->uri->rsegment(<var>n</var>)</h2>
<p>This function is identical to the previous one, except that it lets you retrieve a specific segment from your
re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
<h2>$this->uri->slash_segment(<var>n</var>)</h2>
<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
parameter. If the parameter is not used, a trailing slash added. Examples:</p>
<code>$this->uri->slash_segment(<var>3</var>);<br />
$this->uri->slash_segment(<var>3</var>, 'leading');<br />
$this->uri->slash_segment(<var>3</var>, 'both');</code>
<p>Returns:</p>
<ol>
<li>segment/</li>
<li>/segment</li>
<li>/segment/</li>
</ol>
<h2>$this->uri->slash_rsegment(<var>n</var>)</h2>
<p>This function is identical to the previous one, except that it lets you add slashes a specific segment from your
re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
<h2>$this->uri->uri_to_assoc(<var>n</var>)</h2>
<p>This function lets you turn URI segments into and associative array of key/value pairs. Consider this URI:</p>
<code>index.php/user/search/name/joe/location/UK/gender/male</code>
<p>Using this function you can turn the URI into an associative array with this prototype:</p>
<code>[array]<br />
(<br />
'name' => 'joe'<br />
'location' => 'UK'<br />
'gender' => 'male'<br />
)</code>
<p>The first parameter of the function lets you set an offset. By default it is set to <kbd>3</kbd> since your
URI will normally contain a controller/function in the first and second segments. Example:</p>
<code>
$array = $this->uri->uri_to_assoc(3);<br />
<br />
echo $array['name'];
</code>
<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>
<code>
$default = array('name', 'gender', 'location', 'type', 'sort');<br />
<br />
$array = $this->uri->uri_to_assoc(3, $default);</code>
<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>
<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>
<h2>$this->uri->ruri_to_assoc(<var>n</var>)</h2>
<p>This function is identical to the previous one, except that it creates an associative array using the
re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
<h2>$this->uri->assoc_to_uri()</h2>
<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>
<code>$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');<br />
<br />
$str = $this->uri->assoc_to_uri($array);<br />
<br />
// Produces: product/shoes/size/large/color/red
</code>
<h2>$this->uri->uri_string()</h2>
<p>Returns a string with the complete URI. For example, if this is your full URL:</p>
<code>http://example.com/index.php/news/local/345</code>
<p>The function would return this:</p>
<code>news/local/345</code>
<h2>$this->uri->ruri_string()</h2>
<p>This function is identical to the previous one, except that it returns the
re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
<h2>$this->uri->total_segments()</h2>
<p>Returns the total number of segments.</p>
<h2>$this->uri->total_rsegments()</h2>
<p>This function is identical to the previous one, except that it returns the total number of segments in your
re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
<h2>$this->uri->segment_array()</h2>
<p>Returns an array containing the URI segments. For example:</p>
<code>
$segs = $this->uri->segment_array();<br />
<br />
foreach ($segs as $segment)<br />
{<br />
echo $segment;<br />
echo '<br />';<br />
}</code>
<h2>$this->uri->rsegment_array()</h2>
<p>This function is identical to the previous one, except that it returns the array of segments in your
re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
</div>
<!-- END CONTENT -->
<div id="footer">
<p>
Previous Topic: <a href="unit_testing.html">Unit Testing Class</a>
·
<a href="#top">Top of Page</a> ·
<a href="../index.html">User Guide Home</a> ·
Next Topic: <a href="user_agent.html">User Agent Class</a>
</p>
<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2012 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
</div>
</body>
</html>
|