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 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 25 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 36 | <td><h1>CodeIgniter User Guide Version 1.5.3</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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">
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 48 | <a href="http://www.codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | URI Routing
|
| 51 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 52 | <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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 65 | <p>Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 66 | The 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 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 70 | <p>In some instances, however, you may want to remap this relationship so that a different class/function can be called
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 71 | instead 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>
|
| 76 | www.your-site.com/product/1/<br />
|
| 77 | www.your-site.com/product/2/<br />
|
| 78 | www.your-site.com/product/3/<br />
|
| 79 | www.your-site.com/product/4/
|
| 80 | </p>
|
| 81 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 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.
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 83 | To overcome this, CodeIgniter allows you to remap the URI handler.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 84 |
|
| 85 |
|
| 86 | <h2>Setting your own routing rules</h2>
|
| 87 |
|
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 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
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 89 | permits you to specify your own routing criteria. Routes can either be specified using <dfn>wildcards</dfn> or <dfn>Regular Expressions</dfn></p>
|
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 90 |
|
| 91 |
|
| 92 | <h2>Wildcards</h2>
|
| 93 |
|
| 94 | <p>A typical wildcard route might look something like this:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 95 |
|
| 96 | <code>$route['product/:num'] = "catalog/product_lookup";</code>
|
| 97 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 98 | <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.
|
| 99 | In 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,
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 100 | the "catalog" class and the "product_lookup" method are instead used.</p>
|
| 101 |
|
| 102 | <p>You can match literal values or you can use two wildcard types:</p>
|
| 103 |
|
| 104 | <p>
|
| 105 | :num<br />
|
| 106 | :any
|
| 107 | </p>
|
| 108 |
|
| 109 | <p><strong>:num</strong> will match a segment containing only numbers.<br />
|
| 110 | <strong>:any</strong> will match a segment containing any character.
|
| 111 | </p>
|
| 112 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 113 | <p class="important"><strong>Note:</strong> Routes will run in the order they are defined.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 114 | Higher routes will always take precedence over lower ones.</p>
|
| 115 |
|
| 116 |
|
| 117 | <h2>Examples</h2>
|
| 118 |
|
| 119 | <p>Here are a few routing examples:</p>
|
| 120 |
|
| 121 | <code>$route['journals'] = "blogs";</code>
|
| 122 | <p>Any URL containing the word "journals" in the first segment will be remapped to the "blogs" class.</p>
|
| 123 |
|
| 124 | <code>$route['blog/joe'] = "blogs/users/34";</code>
|
| 125 | <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>
|
| 126 |
|
| 127 |
|
| 128 | <code>$route['product/:any'] = "catalog/product_lookup";</code>
|
| 129 | <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>
|
| 130 |
|
| 131 | <p class="important"><strong>Important:</strong> Do not use leading/trailing slashes.</p>
|
| 132 |
|
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 133 | <h2>Regular Expressions</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 134 |
|
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 135 | <p>If you prefer you can use regular expressions to define your routing rules. Any valid regular expression is allowed, as are back-references.</p>
|
| 136 |
|
| 137 | <p class="important"><strong>Note:</strong> If you use back-references you must use the dollar syntax rather then the double backslash syntax.</p>
|
| 138 |
|
| 139 | <p>A typical RegEx route might look something like this:</p>
|
| 140 |
|
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 141 | <code>$route['products/([a-z]+)/(\d+)'] = "$1/id_$2";</code>
|
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 142 |
|
| 143 | <p>In the above example, a URI similar to <dfn>products/shirts/123</dfn> would instead call the <dfn>shirts</dfn> controller class and the <dfn>id_123</dfn> function.</p>
|
| 144 |
|
| 145 | <p>You can also mix and match wildcards with regular expressions.</p>
|
| 146 |
|
| 147 | <h2>Reserved Routes</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 148 |
|
| 149 | <p>There are two reserved routes:</p>
|
| 150 |
|
| 151 | <code>$route['default_controller'] = 'welcome';</code>
|
| 152 |
|
| 153 | <p>This route indicates which controller class should be loaded if the URI contains no data, which will be the case
|
| 154 | when people load your root URL. In the above example, the "welcome" class would be loaded. You
|
| 155 | are encouraged to always have a default route otherwise a 404 page will appear by default.</p>
|
| 156 |
|
| 157 | <code>$route['scaffolding_trigger'] = 'scaffolding';</code>
|
| 158 |
|
| 159 | <p>This route lets you set a secret word, which when present in the URL, triggers the scaffolding feature.
|
| 160 | Please read the <a href="scaffolding.html">Scaffolding</a> page for details.</p>
|
| 161 |
|
Derek Allard | 24d9544 | 2007-04-30 12:21:37 +0000 | [diff] [blame] | 162 | <p class="important"><strong>Important:</strong> The reserved routes must come before any wildcard or regular expression routes.</p>
|
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 163 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 164 | </div>
|
| 165 | <!-- END CONTENT -->
|
| 166 |
|
| 167 |
|
| 168 | <div id="footer">
|
| 169 | <p>
|
| 170 | Previous Topic: <a href="scaffolding.html">Scaffolding</a>
|
| 171 | ·
|
| 172 | <a href="#top">Top of Page</a> ·
|
| 173 | <a href="../index.html">User Guide Home</a> ·
|
| 174 | Next Topic: <a href="errors.html">Error Handling</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 175 | </p>
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 176 | <p><a href="http://www.codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 177 | </div>
|
| 178 |
|
| 179 | </body>
|
| 180 | </html> |