blob: 0a16e13af42671678e2bd6a022f5cad7bcf43e53 [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
Jonatas Miguelcf168302012-08-06 17:10:17 +0100109Callbacks
110=========
111
112If you are using PHP >= 5.3 you can use callbacks in place of the normal routing
113rules to process the back-references. Example::
114
Jonatas Miguelf002c2a2012-08-30 13:56:01 +0200115 $route['products/([a-z]+)/edit/(\d+)'] = function ($product_type, $id)
116 {
Jonatas Miguelcf168302012-08-06 17:10:17 +0100117 return "catalog/product_edit/" . strtolower($product_type) . "/" . $id;
118 };
119
Derek Jones8ede1a22011-10-05 13:34:52 -0500120Reserved Routes
121===============
122
123There are two reserved routes::
124
125 $route['default_controller'] = 'welcome';
126
127This route indicates which controller class should be loaded if the URI
128contains no data, which will be the case when people load your root URL.
129In the above example, the "welcome" class would be loaded. You are
130encouraged to always have a default route otherwise a 404 page will
131appear by default.
132
133::
134
135 $route['404_override'] = '';
136
137This route indicates which controller class should be loaded if the
138requested controller is not found. It will override the default 404
139error page. It won't affect to the show_404() function, which will
140continue loading the default error_404.php file at
141application/errors/error_404.php.
142
143.. important:: The reserved routes must come before any wildcard or
144 regular expression routes.