Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | URI Routing |
| 3 | ########### |
| 4 | |
| 5 | Typically there is a one-to-one relationship between a URL string and |
| 6 | its corresponding controller class/method. The segments in a URI |
| 7 | normally follow this pattern:: |
| 8 | |
| 9 | example.com/class/function/id/ |
| 10 | |
| 11 | In some instances, however, you may want to remap this relationship so |
| 12 | that a different class/function can be called instead of the one |
| 13 | corresponding to the URL. |
| 14 | |
| 15 | For example, lets say you want your URLs to have this prototype: |
| 16 | |
| 17 | example.com/product/1/ |
| 18 | example.com/product/2/ |
| 19 | example.com/product/3/ |
| 20 | example.com/product/4/ |
| 21 | |
| 22 | Normally the second segment of the URL is reserved for the function |
| 23 | name, but in the example above it instead has a product ID. To overcome |
| 24 | this, CodeIgniter allows you to remap the URI handler. |
| 25 | |
| 26 | Setting your own routing rules |
| 27 | ============================== |
| 28 | |
| 29 | Routing rules are defined in your application/config/routes.php file. In |
| 30 | it you'll see an array called $route that permits you to specify your |
| 31 | own routing criteria. Routes can either be specified using wildcards or |
| 32 | Regular Expressions |
| 33 | |
| 34 | Wildcards |
| 35 | ========= |
| 36 | |
| 37 | A typical wildcard route might look something like this:: |
| 38 | |
| 39 | $route['product/:num'] = "catalog/product_lookup"; |
| 40 | |
| 41 | In a route, the array key contains the URI to be matched, while the |
| 42 | array value contains the destination it should be re-routed to. In the |
| 43 | above example, if the literal word "product" is found in the first |
| 44 | segment of the URL, and a number is found in the second segment, the |
| 45 | "catalog" class and the "product_lookup" method are instead used. |
| 46 | |
| 47 | You can match literal values or you can use two wildcard types: |
| 48 | |
| 49 | **(:num)** will match a segment containing only numbers. |
| 50 | **(:any)** will match a segment containing any character. |
| 51 | |
| 52 | .. note:: Routes will run in the order they are defined. Higher routes |
| 53 | will always take precedence over lower ones. |
| 54 | |
| 55 | Examples |
| 56 | ======== |
| 57 | |
| 58 | Here are a few routing examples:: |
| 59 | |
| 60 | $route['journals'] = "blogs"; |
| 61 | |
| 62 | A URL containing the word "journals" in the first segment will be |
| 63 | remapped to the "blogs" class. |
| 64 | |
| 65 | :: |
| 66 | |
| 67 | $route['blog/joe'] = "blogs/users/34"; |
| 68 | |
| 69 | A URL containing the segments blog/joe will be remapped to the "blogs" |
| 70 | class and the "users" method. The ID will be set to "34". |
| 71 | |
| 72 | :: |
| 73 | |
| 74 | $route['product/(:any)'] = "catalog/product_lookup"; |
| 75 | |
| 76 | A URL with "product" as the first segment, and anything in the second |
| 77 | will be remapped to the "catalog" class and the "product_lookup" |
| 78 | method. |
| 79 | |
| 80 | :: |
| 81 | |
| 82 | $route['product/(:num)'] = "catalog/product_lookup_by_id/$1"; |
| 83 | |
| 84 | A URL with "product" as the first segment, and a number in the second |
| 85 | will be remapped to the "catalog" class and the |
| 86 | "product_lookup_by_id" method passing in the match as a variable to |
| 87 | the function. |
| 88 | |
| 89 | .. important:: Do not use leading/trailing slashes. |
| 90 | |
| 91 | Regular Expressions |
| 92 | =================== |
| 93 | |
| 94 | If you prefer you can use regular expressions to define your routing |
| 95 | rules. Any valid regular expression is allowed, as are back-references. |
| 96 | |
| 97 | .. note:: If you use back-references you must use the dollar syntax |
| 98 | rather than the double backslash syntax. |
| 99 | |
| 100 | A typical RegEx route might look something like this:: |
| 101 | |
| 102 | $route['products/([a-z]+)/(\d+)'] = "$1/id_$2"; |
| 103 | |
| 104 | In the above example, a URI similar to products/shirts/123 would instead |
| 105 | call the shirts controller class and the id_123 function. |
| 106 | |
| 107 | You can also mix and match wildcards with regular expressions. |
| 108 | |
| 109 | Reserved Routes |
| 110 | =============== |
| 111 | |
| 112 | There are two reserved routes:: |
| 113 | |
| 114 | $route['default_controller'] = 'welcome'; |
| 115 | |
| 116 | This route indicates which controller class should be loaded if the URI |
| 117 | contains no data, which will be the case when people load your root URL. |
| 118 | In the above example, the "welcome" class would be loaded. You are |
| 119 | encouraged to always have a default route otherwise a 404 page will |
| 120 | appear by default. |
| 121 | |
| 122 | :: |
| 123 | |
| 124 | $route['404_override'] = ''; |
| 125 | |
| 126 | This route indicates which controller class should be loaded if the |
| 127 | requested controller is not found. It will override the default 404 |
| 128 | error page. It won't affect to the show_404() function, which will |
| 129 | continue loading the default error_404.php file at |
| 130 | application/errors/error_404.php. |
| 131 | |
| 132 | .. important:: The reserved routes must come before any wildcard or |
| 133 | regular expression routes. |