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