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 |
|
| 10 | <script type="text/javascript" src="../scripts/nav.js"></script>
|
| 11 | <script type="text/javascript" src="../scripts/prototype.lite.js"></script>
|
| 12 | <script type="text/javascript" src="../scripts/moo.fx.js"></script>
|
| 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 | 9fc347d | 2006-09-21 00:00:28 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.4.1</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 |
|
| 66 | <p>The URI Class provides functions that help you retrieve information from your URI strings.</p>
|
| 67 |
|
| 68 | <p class="important"><strong>Note:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
|
| 69 |
|
| 70 | <h2>$this->uri->segment(<var>n</var>)</h2>
|
| 71 |
|
| 72 | <p>Permits you to retrieve a specific segment. Where <var>n</var> is the segment number you wish to retrieve.
|
| 73 | Segments are numbered from left to right. For example, if your full URL is this:</p>
|
| 74 |
|
| 75 | <code>http://www.your-site.com/index.php/news/local/metro/crime_is_up</code>
|
| 76 |
|
| 77 | <p>The segment numbers would be this:</p>
|
| 78 |
|
| 79 | <ol>
|
| 80 | <li>news</li>
|
| 81 | <li>local</li>
|
| 82 | <li>metro</li>
|
| 83 | <li>crime_is_up</li>
|
| 84 | </ol>
|
| 85 |
|
| 86 | <p>By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that
|
| 87 | permits you to set your own default value if the segment is missing.
|
| 88 | For example, this would tell the function to return the number zero in the event of failure:</p>
|
| 89 |
|
| 90 | <code>$product_id = $this->uri->segment(3, 0);</code>
|
| 91 |
|
| 92 | <p>It helps avoid having to write code like this:</p>
|
| 93 |
|
| 94 | <code>if ($this->uri->segment(3) === FALSE)<br />
|
| 95 | {<br />
|
| 96 | $product_id = 0;<br />
|
| 97 | }<br />
|
| 98 | else<br />
|
| 99 | {<br />
|
| 100 | $product_id = $this->uri->segment(3);<br />
|
| 101 | }<br />
|
| 102 | </code>
|
| 103 |
|
| 104 |
|
| 105 | <h2>$this->uri->slash_segment(<var>n</var>)</h2>
|
| 106 |
|
| 107 | <p>This function is almost identical to the one above, except it adds a trailing and/or leading slash based on the second
|
| 108 | parameter. If the parameter is not used, a trailing slash added. Examples:</p>
|
| 109 |
|
| 110 | <code>$this->uri->slash_segment(<var>3</var>);<br />
|
| 111 | $this->uri->slash_segment(<var>3</var>, 'leading');<br />
|
| 112 | $this->uri->slash_segment(<var>3</var>, 'both');</code>
|
| 113 |
|
| 114 | <p>Returns:</p>
|
| 115 |
|
| 116 | <ol>
|
| 117 | <li>segment/</li>
|
| 118 | <li>/segment</li>
|
| 119 | <li>/segment/</li>
|
| 120 | </ol>
|
| 121 |
|
| 122 |
|
| 123 | <h2>$this->uri->uri_to_assoc(<var>n</var>)</h2>
|
| 124 |
|
| 125 | <p>This function lets you turn URI segments into and associative array of key/value pairs. Consider this URI:</p>
|
| 126 |
|
| 127 | <code>index.php/user/search/name/joe/location/UK/gender/male</code>
|
| 128 |
|
| 129 | <p>Using this function you can turn the URI into an associative array with this prototype:</p>
|
| 130 |
|
| 131 | <code>[array]<br />
|
| 132 | (<br />
|
| 133 | 'name' => 'joe'<br />
|
| 134 | 'location' => 'UK'<br />
|
| 135 | 'gender' => 'male'<br />
|
| 136 | )</code>
|
| 137 |
|
| 138 | <p>The first parameter of the function lets you set an offset. By default it is set to <kbd>3</kbd> since your
|
| 139 | URI will normally contain a controller/function in the first and second segments. Example:</p>
|
| 140 |
|
| 141 | <code>
|
| 142 | $array = $this->uri->uri_to_assoc(3);<br />
|
| 143 | <br />
|
| 144 | echo $array['name'];
|
| 145 | </code>
|
| 146 |
|
| 147 |
|
| 148 | <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>
|
| 149 |
|
| 150 | <code>
|
| 151 | $default = array('name', 'gender', 'location', 'type', 'sort');<br />
|
| 152 | <br />
|
| 153 | $array = $this->uri->uri_to_assoc(3, $default);</code>
|
| 154 |
|
| 155 | <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>
|
| 156 |
|
| 157 | <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>
|
| 158 |
|
| 159 |
|
| 160 | <h2>$this->uri->assoc_to_uri()</h2>
|
| 161 |
|
| 162 | <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>
|
| 163 |
|
| 164 | <code>$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');<br />
|
| 165 | <br />
|
| 166 | $str = $this->uri->assoc_to_str($array);<br />
|
| 167 | <br />
|
| 168 | // Produces: product/shoes/size/large/color/red
|
| 169 | </code>
|
| 170 |
|
| 171 |
|
| 172 | <h2>$this->uri->uri_string()</h2>
|
| 173 |
|
| 174 | <p>Returns a string with the complete URI. For example, if this is your full URL:</p>
|
| 175 |
|
| 176 | <code>http://www.your-site.com/index.php/news/local/345</code>
|
| 177 |
|
| 178 | <p>The function would return this:</p>
|
| 179 |
|
| 180 | <code>news/local/345</code>
|
| 181 |
|
| 182 |
|
| 183 | <h2>$this->uri->total_segments()</h2>
|
| 184 |
|
| 185 | <p>Returns the total number of segments.</p>
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 | <h2>$this->uri->segment_array()</h2>
|
| 190 |
|
| 191 | <p>Returns an array containing the URI segments. For example:</p>
|
| 192 |
|
| 193 | <code>
|
| 194 | $segs = $this->uri->segment_array();<br />
|
| 195 | <br />
|
| 196 | foreach ($segs as $segment)<br />
|
| 197 | {<br />
|
| 198 | echo $segment;<br />
|
| 199 | echo '<br />';<br />
|
| 200 | }</code>
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 | </div>
|
| 205 | <!-- END CONTENT -->
|
| 206 |
|
| 207 |
|
| 208 | <div id="footer">
|
| 209 | <p>
|
| 210 | Previous Topic: <a href="parser.html">Template Parser Class</a>
|
| 211 | ·
|
| 212 | <a href="#top">Top of Page</a> ·
|
| 213 | <a href="../index.html">User Guide Home</a> ·
|
| 214 | Next Topic: <a href="validation.html">Validation Class</a>
|
| 215 | <p>
|
| 216 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 217 | </div>
|
| 218 |
|
| 219 | </body>
|
| 220 | </html> |