blob: 6495f1ad4d501dd15825cc07e26314273ecd3bb0 [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
Andrey Andreev16a704c2012-11-09 17:25:00 +020012that a different class/method can be called instead of the one
Derek Jones8ede1a22011-10-05 13:34:52 -050013corresponding to the URL.
14
vlakoff35672462013-02-15 01:36:04 +010015For example, let's say you want your URLs to have this prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050016
Andrey Andreev16a704c2012-11-09 17:25:00 +020017 example.com/product/1/
18 example.com/product/2/
19 example.com/product/3/
20 example.com/product/4/
Derek Jones8ede1a22011-10-05 13:34:52 -050021
Andrey Andreev16a704c2012-11-09 17:25:00 +020022Normally the second segment of the URL is reserved for the method
23name, but in the example above it instead has a product ID. To
24overcome this, CodeIgniter allows you to remap the URI handler.
Derek Jones8ede1a22011-10-05 13:34:52 -050025
26Setting your own routing rules
27==============================
28
Andrey Andreev16a704c2012-11-09 17:25:00 +020029Routing rules are defined in your *application/config/routes.php* file.
30In it you'll see an array called ``$route`` that permits you to specify
31your own routing criteria. Routes can either be specified using wildcards
32or Regular Expressions.
Derek Jones8ede1a22011-10-05 13:34:52 -050033
34Wildcards
35=========
36
37A typical wildcard route might look something like this::
38
Andrey Andreev16a704c2012-11-09 17:25:00 +020039 $route['product/:num'] = 'catalog/product_lookup';
Derek Jones8ede1a22011-10-05 13:34:52 -050040
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
Andrey Andreev16a704c2012-11-09 17:25:00 +020069 $route['journals'] = 'blogs';
Derek Jones8ede1a22011-10-05 13:34:52 -050070
71A URL containing the word "journals" in the first segment will be
72remapped to the "blogs" class.
73
74::
75
Andrey Andreev16a704c2012-11-09 17:25:00 +020076 $route['blog/joe'] = 'blogs/users/34';
Derek Jones8ede1a22011-10-05 13:34:52 -050077
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
Andrey Andreev16a704c2012-11-09 17:25:00 +020083 $route['product/(:any)'] = 'catalog/product_lookup';
Derek Jones8ede1a22011-10-05 13:34:52 -050084
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
Andrey Andreev16a704c2012-11-09 17:25:00 +020091 $route['product/(:num)'] = 'catalog/product_lookup_by_id/$1';
Derek Jones8ede1a22011-10-05 13:34:52 -050092
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
Andrey Andreev16a704c2012-11-09 17:25:00 +020096the method.
Derek Jones8ede1a22011-10-05 13:34:52 -050097
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 Andreev16a704c2012-11-09 17:25:00 +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
Andrey Andreev16a704c2012-11-09 17:25:00 +0200125That will call the "auth" controller class and its ``login()`` method,
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200126passing 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
vlakoff0c1e1632013-01-28 21:24:36 +0100132.. 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
Andrey Andreev16a704c2012-11-09 17:25:00 +0200137If you are using PHP >= 5.3 you can use callbacks in place of the normal
138routing rules to process the back-references. Example::
Jonatas Miguelcf168302012-08-06 17:10:17 +0100139
vlakoff0c1e1632013-01-28 21:24:36 +0100140 $route['products/([a-zA-Z]+)/edit/(\d+)'] = function ($product_type, $id)
Jonatas Miguel8f1cdd12012-08-30 13:57:54 +0200141 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200142 return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
Jonatas Miguelcf168302012-08-06 17:10:17 +0100143 };
144
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700145Using HTTP Verb in Routes
146=========================
147
148If you prefer you can use HTTP Verb (or method) to define your routing rules.
149This is particularly useful when building RESTful application. You can use standard HTTP
150Verb (GET, PUT, POST, DELETE) or custom HTTP Verb (e.g: PURGE). HTTP Verb rule is case
151insensitive. All you need to do is add array index using HTTP Verb rule. Example::
152
153 $route['products']['put'] = 'product/insert';
154
155In the above example, a PUT request to URI "products" would call the "product" controller
156class and "insert" method
157
158::
159
160 $route['products/(:num)']['DELETE'] = 'product/delete/$1';
161
162A DELETE request to URL with "products" as first segment and a number in the second will be
163remapped to the "product" class and "delete" method passing in the match as a variable to
164the method.
165
166::
167
168 $route['products/([a-z]+)/(\d+)']['get'] = 'product/$1/$2';
169
170A GET request to a URI similar to products/shirts/123 would call the "product" controller
171class and "shirt" method with number as method parameter
172
173Using HTTP Verb is optional, so if you want any HTTP Verb to be handled in one rule
174You could just write your routing rule without HTTP Verb. Example::
175
176 $route['product'] = 'product';
177
178This way, all incoming request using any HTTP method containing the word "product"
179in the first segment will be remapped to "product" class
180
Derek Jones8ede1a22011-10-05 13:34:52 -0500181Reserved Routes
182===============
183
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300184There are three reserved routes::
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
186 $route['default_controller'] = 'welcome';
187
188This route indicates which controller class should be loaded if the URI
189contains no data, which will be the case when people load your root URL.
190In the above example, the "welcome" class would be loaded. You are
191encouraged to always have a default route otherwise a 404 page will
192appear by default.
193
194::
195
196 $route['404_override'] = '';
197
198This route indicates which controller class should be loaded if the
199requested controller is not found. It will override the default 404
Andrey Andreev16a704c2012-11-09 17:25:00 +0200200error page. It won't affect to the ``show_404()`` function, which will
Andrey Andreevd1097a12012-11-01 19:55:42 +0200201continue loading the default *error_404.php* file at
vlakoff52301c72013-03-29 14:23:34 +0100202*application/views/errors/error_404.php*.
Derek Jones8ede1a22011-10-05 13:34:52 -0500203
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300204
205::
206
207 $route['translate_uri_dashes'] = FALSE;
208
209As evident by the boolean value, this is not exactly a route. This
210option enables you to automatically replace dashes ('-') with
211underscores in the controller and method URI segments, thus saving you
212additional route entries if you need to do that.
213This is required, because the dash isn't a valid class or method name
214character and would cause a fatal error if you try to use it.
215
Derek Jones8ede1a22011-10-05 13:34:52 -0500216.. important:: The reserved routes must come before any wildcard or
Andrey Andreevd1097a12012-11-01 19:55:42 +0200217 regular expression routes.