blob: 909289d8d92632348bc5cc611574d137038bfaef [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 Andreev01d53bd2015-11-23 13:09:22 +0200116With regular expressions, you can also catch multiple segments at once.
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200117For example, if a user accesses a password protected area of your web
118application and you wish to be able to redirect them back to the same
119page after they log in, you may find this example useful::
120
121 $route['login/(.+)'] = 'auth/login/$1';
122
Andrey Andreev01d53bd2015-11-23 13:09:22 +0200123.. note:: In the above example, if the ``$1`` placeholder contains a
124 slash, it will still be split into multiple parameters when
125 passed to ``Auth::login()``.
126
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200127For those of you who don't know regular expressions and want to learn
Andrey Andreev01d53bd2015-11-23 13:09:22 +0200128more about them, `regular-expressions.info <http://www.regular-expressions.info/>`_
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200129might be a good starting point.
130
vlakoff0c1e1632013-01-28 21:24:36 +0100131.. note:: You can also mix and match wildcards with regular expressions.
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Jonatas Miguelcf168302012-08-06 17:10:17 +0100133Callbacks
134=========
135
Andrey Andreeva8382792016-07-28 16:40:12 +0300136You can also use callbacks in place of the normal routing rules to process
137the back-references. Example::
Jonatas Miguelcf168302012-08-06 17:10:17 +0100138
vlakoff0c1e1632013-01-28 21:24:36 +0100139 $route['products/([a-zA-Z]+)/edit/(\d+)'] = function ($product_type, $id)
Jonatas Miguel8f1cdd12012-08-30 13:57:54 +0200140 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200141 return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
Jonatas Miguelcf168302012-08-06 17:10:17 +0100142 };
143
Andrey Andreevc761a202013-11-11 14:02:15 +0200144Using HTTP verbs in routes
145==========================
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700146
Andrey Andreevc761a202013-11-11 14:02:15 +0200147It is possible to use HTTP verbs (request method) to define your routing rules.
148This is particularly useful when building RESTful applications. You can use standard HTTP
149verbs (GET, PUT, POST, DELETE, PATCH) or a custom one such (e.g. PURGE). HTTP verb rules
150are case-insensitive. All you need to do is to add the verb as an array key to your route.
151Example::
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700152
153 $route['products']['put'] = 'product/insert';
154
Andrey Andreevc761a202013-11-11 14:02:15 +0200155In the above example, a PUT request to URI "products" would call the ``Product::insert()``
156controller method.
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700157
158::
159
160 $route['products/(:num)']['DELETE'] = 'product/delete/$1';
161
Andrey Andreevc761a202013-11-11 14:02:15 +0200162A DELETE request to URL with "products" as first the segment and a number in the second will be
163mapped to the ``Product::delete()`` method, passing the numeric value as the first parameter.
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700164
Andrey Andreevc761a202013-11-11 14:02:15 +0200165Using HTTP verbs is of course, optional.
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700166
Derek Jones8ede1a22011-10-05 13:34:52 -0500167Reserved Routes
168===============
169
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300170There are three reserved routes::
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
172 $route['default_controller'] = 'welcome';
173
Andrey Andreevee2ece82015-09-28 13:18:05 +0300174This route points to the action that should be executed if the URI contains
175no data, which will be the case when people load your root URL.
176The setting accepts a **controller/method** value and ``index()`` would be
177the default method if you don't specify one. In the above example, it is
178``Welcome::index()`` that would be called.
179
180.. note:: You can NOT use a directory as a part of this setting!
181
182You are encouraged to always have a default route as otherwise a 404 page
183will appear by default.
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
185::
186
187 $route['404_override'] = '';
188
189This route indicates which controller class should be loaded if the
190requested controller is not found. It will override the default 404
Andrey Andreev7f5c7532015-09-28 17:05:52 +0300191error page. Same per-directory rules as with 'default_controller'
192apply here as well.
193
194It won't affect to the ``show_404()`` function, which will
Andrey Andreevd1097a12012-11-01 19:55:42 +0200195continue loading the default *error_404.php* file at
vlakoff52301c72013-03-29 14:23:34 +0100196*application/views/errors/error_404.php*.
Derek Jones8ede1a22011-10-05 13:34:52 -0500197
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300198::
199
200 $route['translate_uri_dashes'] = FALSE;
201
202As evident by the boolean value, this is not exactly a route. This
203option enables you to automatically replace dashes ('-') with
204underscores in the controller and method URI segments, thus saving you
205additional route entries if you need to do that.
206This is required, because the dash isn't a valid class or method name
207character and would cause a fatal error if you try to use it.