blob: 367e1b798a2745686d3d097924d52471fe509e9c [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Code Igniter 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">
window.onload = function() {
myHeight = new fx.Height('nav', {duration: 400});
myHeight.hide();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='Rick Ellis' />
<meta name='description' content='Code Igniter 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.jpg" width="153" height="44" 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>Code Igniter User Guide Version 1.5.0b3</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</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://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
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="www.codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<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://www.your-site.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 />
&nbsp;&nbsp;&nbsp;&nbsp;$product_id = 0;<br />
}<br />
else<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;$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 Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
<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 Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
<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 />
&nbsp;&nbsp;&nbsp;&nbsp;'name' => 'joe'<br />
&nbsp;&nbsp;&nbsp;&nbsp;'location' => 'UK'<br />
&nbsp;&nbsp;&nbsp;&nbsp;'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 Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
<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_str($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://www.your-site.com/index.php/news/local/345</code>
<p>The function would return this:</p>
<code>news/local/345</code>
<h2>$this->uri->ruri_string(<var>n</var>)</h2>
<p>This function is identical to the previous one, except that it returns the
re-routed URI in the event you are using Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
<h2>$this->uri->total_segments()</h2>
<p>Returns the total number of segments.</p>
<h2>$this->uri->total_rsegments(<var>n</var>)</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 Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
<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 />
&nbsp;&nbsp;&nbsp;&nbsp;echo $segment;<br />
&nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;br />';<br />
}</code>
<h2>$this->uri->rsegment_array(<var>n</var>)</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 Code Igniter's <a href="../general/routing.html">URI Routing</a> feature.
</div>
<!-- END CONTENT -->
<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="parser.html">Template Parser Class</a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="user_agent.html">User Agent Class</a>
<p>
<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
</div>
</body>
</html>