blob: e6174cc0dee4a77dea597de3283c7e6161b01087 [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
Andrey Andreev7676c2d2012-10-30 13:42:01 +020032Regular Expressions.
Derek Jones8ede1a22011-10-05 13:34:52 -050033
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.
Andrey Andreev7676c2d2012-10-30 13:42:01 +020050**(:any)** will match a segment containing any character (except for '/', which is the segment delimiter).
51
52.. note:: Wildcards are actually aliases for regular expressions, with
53 **:any** being translated to **[^/]+** and **:num** to **[0-9]+**,
54 respectively.
Derek Jones8ede1a22011-10-05 13:34:52 -050055
56.. note:: Routes will run in the order they are defined. Higher routes
57 will always take precedence over lower ones.
58
Andrey Andreeva9d98ce2012-10-23 10:05:31 +030059.. note:: Route rules are not filters! Setting a rule of e.g.
60 'foo/bar/(:num)' will not prevent controller *Foo* and method
61 *bar* to be called with a non-numeric value if that is a valid
62 route.
63
Derek Jones8ede1a22011-10-05 13:34:52 -050064Examples
65========
66
67Here are a few routing examples::
68
69 $route['journals'] = "blogs";
70
71A URL containing the word "journals" in the first segment will be
72remapped to the "blogs" class.
73
74::
75
76 $route['blog/joe'] = "blogs/users/34";
77
78A URL containing the segments blog/joe will be remapped to the "blogs"
79class and the "users" method. The ID will be set to "34".
80
81::
82
83 $route['product/(:any)'] = "catalog/product_lookup";
84
85A URL with "product" as the first segment, and anything in the second
86will be remapped to the "catalog" class and the "product_lookup"
87method.
88
89::
90
91 $route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
92
93A URL with "product" as the first segment, and a number in the second
94will be remapped to the "catalog" class and the
95"product_lookup_by_id" method passing in the match as a variable to
96the function.
97
98.. important:: Do not use leading/trailing slashes.
99
100Regular Expressions
101===================
102
103If you prefer you can use regular expressions to define your routing
104rules. Any valid regular expression is allowed, as are back-references.
105
106.. note:: If you use back-references you must use the dollar syntax
107 rather than the double backslash syntax.
108
109A typical RegEx route might look something like this::
110
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200111 $route['products/([a-z]+)/(\d+)'] = '$1/id_$2';
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
113In the above example, a URI similar to products/shirts/123 would instead
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200114call the shirts controller class and the id_123 method.
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200116With regular expressions, you can also catch a segment containing a
117forward slash ('/'), which would usually represent the delimiter between
118multiple segments.
119For example, if a user accesses a password protected area of your web
120application and you wish to be able to redirect them back to the same
121page after they log in, you may find this example useful::
122
123 $route['login/(.+)'] = 'auth/login/$1';
124
125That will call the auth controller class and its ``login()`` method,
126passing everything contained in the URI after *login/* as a parameter.
127
128For those of you who don't know regular expressions and want to learn
129more about them, `regular-expressions.info <http://www.regular-expressions.info/>`
130might be a good starting point.
131
132..note:: You can also mix and match wildcards with regular expressions.
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Jonatas Miguelcf168302012-08-06 17:10:17 +0100134Callbacks
135=========
136
137If you are using PHP >= 5.3 you can use callbacks in place of the normal routing
138rules to process the back-references. Example::
139
Jonatas Miguelf002c2a2012-08-30 13:56:01 +0200140 $route['products/([a-z]+)/edit/(\d+)'] = function ($product_type, $id)
Jonatas Miguel8f1cdd12012-08-30 13:57:54 +0200141 {
Jonatas Miguelcf168302012-08-06 17:10:17 +0100142 return "catalog/product_edit/" . strtolower($product_type) . "/" . $id;
143 };
144
Derek Jones8ede1a22011-10-05 13:34:52 -0500145Reserved Routes
146===============
147
148There are two reserved routes::
149
150 $route['default_controller'] = 'welcome';
151
152This route indicates which controller class should be loaded if the URI
153contains no data, which will be the case when people load your root URL.
154In the above example, the "welcome" class would be loaded. You are
155encouraged to always have a default route otherwise a 404 page will
156appear by default.
157
158::
159
160 $route['404_override'] = '';
161
162This route indicates which controller class should be loaded if the
163requested controller is not found. It will override the default 404
164error page. It won't affect to the show_404() function, which will
Andrey Andreevd1097a12012-11-01 19:55:42 +0200165continue loading the default *error_404.php* file at
166*application/errors/error_404.php*.
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
168.. important:: The reserved routes must come before any wildcard or
Andrey Andreevd1097a12012-11-01 19:55:42 +0200169 regular expression routes.