blob: 0b91d3fa9595add112e6f1a30fae838b67e887a8 [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
Andrey Andreevc761a202013-11-11 14:02:15 +0200145Using HTTP verbs in routes
146==========================
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700147
Andrey Andreevc761a202013-11-11 14:02:15 +0200148It is possible to use HTTP verbs (request method) to define your routing rules.
149This is particularly useful when building RESTful applications. You can use standard HTTP
150verbs (GET, PUT, POST, DELETE, PATCH) or a custom one such (e.g. PURGE). HTTP verb rules
151are case-insensitive. All you need to do is to add the verb as an array key to your route.
152Example::
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700153
154 $route['products']['put'] = 'product/insert';
155
Andrey Andreevc761a202013-11-11 14:02:15 +0200156In the above example, a PUT request to URI "products" would call the ``Product::insert()``
157controller method.
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700158
159::
160
161 $route['products/(:num)']['DELETE'] = 'product/delete/$1';
162
Andrey Andreevc761a202013-11-11 14:02:15 +0200163A DELETE request to URL with "products" as first the segment and a number in the second will be
164mapped to the ``Product::delete()`` method, passing the numeric value as the first parameter.
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700165
Andrey Andreevc761a202013-11-11 14:02:15 +0200166Using HTTP verbs is of course, optional.
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700167
Derek Jones8ede1a22011-10-05 13:34:52 -0500168Reserved Routes
169===============
170
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300171There are three reserved routes::
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
173 $route['default_controller'] = 'welcome';
174
175This route indicates which controller class should be loaded if the URI
176contains no data, which will be the case when people load your root URL.
177In the above example, the "welcome" class would be loaded. You are
178encouraged to always have a default route otherwise a 404 page will
179appear by default.
180
181::
182
183 $route['404_override'] = '';
184
185This route indicates which controller class should be loaded if the
186requested controller is not found. It will override the default 404
Andrey Andreev16a704c2012-11-09 17:25:00 +0200187error page. It won't affect to the ``show_404()`` function, which will
Andrey Andreevd1097a12012-11-01 19:55:42 +0200188continue loading the default *error_404.php* file at
vlakoff52301c72013-03-29 14:23:34 +0100189*application/views/errors/error_404.php*.
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300191
192::
193
194 $route['translate_uri_dashes'] = FALSE;
195
196As evident by the boolean value, this is not exactly a route. This
197option enables you to automatically replace dashes ('-') with
198underscores in the controller and method URI segments, thus saving you
199additional route entries if you need to do that.
200This is required, because the dash isn't a valid class or method name
201character and would cause a fatal error if you try to use it.
202
Derek Jones8ede1a22011-10-05 13:34:52 -0500203.. important:: The reserved routes must come before any wildcard or
Andrey Andreevd1097a12012-11-01 19:55:42 +0200204 regular expression routes.