blob: c6e8bd9cf258c070eae6c3eb4c8cfdaac07d6586 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!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">
14window.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>
36<td><h1>Code Igniter User Guide Version 1.4.0</h1></td>
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> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50URI Routing
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&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>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63<h1>URI Routing</h1>
64
65<p>Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method.
66The segments in a URI normally follow this pattern:</p>
67
68<code>www.your-site.com/<dfn>class</dfn>/<samp>function</samp>/<var>id</var>/</code>
69
70<p>In some instances, however, you may want to remap this relationship so that a different class/function can be called
71instead of the one corresponding to the URL.</p>
72
73<p>For example, lets say you want your URLs to have this prototype:</p>
74
75<p>
76www.your-site.com/product/1/<br />
77www.your-site.com/product/2/<br />
78www.your-site.com/product/3/<br />
79www.your-site.com/product/4/
80</p>
81
82<p>Normally the second segment of the URL is reserved for the function name, but in the example above, it instead has a product ID.
83To overcome this, Code Igniter allows you to remap the URI handler.</p>
84
85
86<h2>Setting your own routing rules</h2>
87
88<p>Routing rules are defined in your <var>application/config/routes.php</var> file. In it you'll see an array called <dfn>$route</dfn>, that
89you can use to specify your own routing criteria. A typical route might look something like this:</p>
90
91<code>$route['product/:num'] = "catalog/product_lookup";</code>
92
93<p>In a route, the array key contains the URI to be matched, while the array value contains the destination it should be re-routed to.
94In the above example, if the literal word "product" is found in the first segment of the URL, and a number is found in the second segment,
95the "catalog" class and the "product_lookup" method are instead used.</p>
96
97<p>You can match literal values or you can use two wildcard types:</p>
98
99<p>
100:num<br />
101:any
102</p>
103
104<p><strong>:num</strong> will match a segment containing only numbers.<br />
105<strong>:any</strong> will match a segment containing any character.
106</p>
107
108<p class="important"><strong>Note:</strong> Routes will run in the order they are defined.
109Higher routes will always take precedence over lower ones.</p>
110
111
112<h2>Examples</h2>
113
114<p>Here are a few routing examples:</p>
115
116<code>$route['journals'] = "blogs";</code>
117<p>Any URL containing the word "journals" in the first segment will be remapped to the "blogs" class.</p>
118
119<code>$route['blog/joe'] = "blogs/users/34";</code>
120<p>Any URL containing the segments blog/joe will be remapped to the "blogs" class and the "users" method. The ID will be set to "34".</p>
121
122
123<code>$route['product/:any'] = "catalog/product_lookup";</code>
124<p>Any URL with "product" as the first segment, and anything in the second will be remapped to the "catalog" class and the "product_lookup" method.</p>
125
126<p class="important"><strong>Important:</strong> Do not use leading/trailing slashes.</p>
127
128
129<h2>Reserved Route</h2>
130
131<p>There are two reserved routes:</p>
132
133<code>$route['default_controller'] = 'welcome';</code>
134
135<p>This route indicates which controller class should be loaded if the URI contains no data, which will be the case
136when people load your root URL. In the above example, the "welcome" class would be loaded. You
137are encouraged to always have a default route otherwise a 404 page will appear by default.</p>
138
139<code>$route['scaffolding_trigger'] = 'scaffolding';</code>
140
141<p>This route lets you set a secret word, which when present in the URL, triggers the scaffolding feature.
142Please read the <a href="scaffolding.html">Scaffolding</a> page for details.</p>
143
144
145
146
147</div>
148<!-- END CONTENT -->
149
150
151<div id="footer">
152<p>
153Previous Topic:&nbsp;&nbsp;<a href="scaffolding.html">Scaffolding</a>
154&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
155<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
156<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
157Next Topic:&nbsp;&nbsp;<a href="errors.html">Error Handling</a>
158<p>
159<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>
160</div>
161
162</body>
163</html>