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