1 <!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">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>URI Class : CodeIgniter User Guide</title>
8 <style type='text/css' media='all'>@import url('../userguide.css');</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
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>
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' />
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>
29 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
36 <!-- END NAVIGATION -->
39 <!-- START BREADCRUMB -->
40 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
43 <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
44 <a href="../index.html">User Guide Home</a> ›
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 <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>
50 <!-- END BREADCRUMB -->
55 <!-- START CONTENT -->
61 <p>The URI Class provides functions that help you retrieve information from your URI strings. If you use URI routing, you can
62 also retrieve information about the re-routed segments.</p>
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>
66 <h2>$this->uri->segment(<var>n</var>)</h2>
68 <p>Permits you to retrieve a specific segment. Where <var>n</var> is the segment number you wish to retrieve.
69 Segments are numbered from left to right. For example, if your full URL is this:</p>
71 <code>http://example.com/index.php/news/local/metro/crime_is_up</code>
73 <p>The segment numbers would be this:</p>
82 <p>By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that
83 permits you to set your own default value if the segment is missing.
84 For example, this would tell the function to return the number zero in the event of failure:</p>
86 <code>$product_id = $this->uri->segment(3, 0);</code>
88 <p>It helps avoid having to write code like this:</p>
90 <code>if ($this->uri->segment(3) === FALSE)<br />
92 $product_id = 0;<br />
96 $product_id = $this->uri->segment(3);<br />
100 <h2>$this->uri->rsegment(<var>n</var>)</h2>
102 <p>This function is identical to the previous one, except that it lets you retrieve a specific segment from your
103 re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
106 <h2>$this->uri->slash_segment(<var>n</var>)</h2>
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
109 parameter. If the parameter is not used, a trailing slash added. Examples:</p>
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>
124 <h2>$this->uri->slash_rsegment(<var>n</var>)</h2>
126 <p>This function is identical to the previous one, except that it lets you add slashes a specific segment from your
127 re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
131 <h2>$this->uri->uri_to_assoc(<var>n</var>)</h2>
133 <p>This function lets you turn URI segments into and associative array of key/value pairs. Consider this URI:</p>
135 <code>index.php/user/search/name/joe/location/UK/gender/male</code>
137 <p>Using this function you can turn the URI into an associative array with this prototype:</p>
141 'name' => 'joe'<br />
142 'location' => 'UK'<br />
143 'gender' => 'male'<br />
146 <p>The first parameter of the function lets you set an offset. By default it is set to <kbd>3</kbd> since your
147 URI will normally contain a controller/function in the first and second segments. Example:</p>
150 $array = $this->uri->uri_to_assoc(3);<br />
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>
159 $default = array('name', 'gender', 'location', 'type', 'sort');<br />
161 $array = $this->uri->uri_to_assoc(3, $default);</code>
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>
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>
168 <h2>$this->uri->ruri_to_assoc(<var>n</var>)</h2>
170 <p>This function is identical to the previous one, except that it creates an associative array using the
171 re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
174 <h2>$this->uri->assoc_to_uri()</h2>
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>
178 <code>$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');<br />
180 $str = $this->uri->assoc_to_uri($array);<br />
182 // Produces: product/shoes/size/large/color/red
186 <h2>$this->uri->uri_string()</h2>
188 <p>Returns a string with the complete URI. For example, if this is your full URL:</p>
190 <code>http://example.com/index.php/news/local/345</code>
192 <p>The function would return this:</p>
194 <code>/news/local/345</code>
197 <h2>$this->uri->ruri_string()</h2>
199 <p>This function is identical to the previous one, except that it returns the
200 re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
204 <h2>$this->uri->total_segments()</h2>
206 <p>Returns the total number of segments.</p>
209 <h2>$this->uri->total_rsegments()</h2>
211 <p>This function is identical to the previous one, except that it returns the total number of segments in your
212 re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
216 <h2>$this->uri->segment_array()</h2>
218 <p>Returns an array containing the URI segments. For example:</p>
221 $segs = $this->uri->segment_array();<br />
223 foreach ($segs as $segment)<br />
225 echo $segment;<br />
226 echo '<br />';<br />
229 <h2>$this->uri->rsegment_array()</h2>
231 <p>This function is identical to the previous one, except that it returns the array of segments in your
232 re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.</p>
242 Previous Topic: <a href="unit_testing.html">Unit Testing Class</a>
243 ·
244 <a href="#top">Top of Page</a> ·
245 <a href="../index.html">User Guide Home</a> ·
246 Next Topic: <a href="user_agent.html">User Agent Class</a>
248 <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p>