Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
Derek Allard | afd99ac | 2008-01-19 19:59:14 +0000 | [diff] [blame^] | 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 3 | <head>
|
| 4 |
|
| 5 | <title>CodeIgniter User Guide : URI Class</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 |
|
| 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
| 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
| 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
| 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
| 14 |
|
| 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' />
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 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.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>
|
| 31 | <td><h1>CodeIgniter User Guide Version 1.6.0</h1></td>
|
| 32 | <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://www.codeigniter.com/">CodeIgniter Home</a> ›
|
| 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | URI 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 <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>
|
| 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
|
| 62 | also 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.
|
| 69 | Segments 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 |
|
| 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>
|
| 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 | $product_id = 0;<br />
|
| 93 | }<br />
|
| 94 | else<br />
|
| 95 | {<br />
|
| 96 | $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
|
| 103 | re-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
|
| 109 | parameter. 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 |
|
| 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
|
| 127 | re-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 |
|
| 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 | 'name' => 'joe'<br />
|
| 142 | 'location' => 'UK'<br />
|
| 143 | '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
|
| 147 | URI 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 />
|
| 152 | echo $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
|
| 171 | re-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 |
|
| 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 />
|
| 180 | $str = $this->uri->assoc_to_uri($array);<br />
|
| 181 | <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 |
|
| 197 | <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
|
| 200 | re-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(<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
|
| 212 | re-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 |
|
| 218 | <p>Returns an array containing the URI segments. For example:</p>
|
| 219 |
|
| 220 | <code>
|
| 221 | $segs = $this->uri->segment_array();<br />
|
| 222 | <br />
|
| 223 | foreach ($segs as $segment)<br />
|
| 224 | {<br />
|
| 225 | echo $segment;<br />
|
| 226 | echo '<br />';<br />
|
| 227 | }</code>
|
| 228 |
|
| 229 | <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
|
| 232 | re-routed URI in the event you are using CodeIgniter's <a href="../general/routing.html">URI Routing</a> feature.
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 | </div>
|
| 237 | <!-- END CONTENT -->
|
| 238 |
|
| 239 |
|
| 240 | <div id="footer">
|
| 241 | <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>
|
| 247 | </p>
|
| 248 | <p><a href="http://www.codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
| 249 | </div>
|
| 250 |
|
| 251 | </body>
|
| 252 | </html> |