How to set CodeIgniter route using HTTP verbs GET, PUT, POST, DELETE, PATCH request for REST API

Set CodeIgniter route using HTTP verbs GET, PUT, POST, DELETE, PATCH request for REST API. Below CodeIgniter route using HTTP verbs or HTTP methods found helpful for RESTful API services using CodeIgniter.

CodeIgniter routing for RESTful API requires extra tricks, as API request accept specified HTTP verbs type. For manage this, we have to use array’s ( $route[ ][ ] ) second dimension.

What is URI Routing – CodeIgniter?

In simple words URI Routing is process where URI used by user is manipulate and serve or make right process for it.

Suppose i use below URI, now how CodeIgniter handle it and execute related code from class is URI Routing process.


http://example.com/user/create

Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow particular pattern.

In some instances, however, you may want to remap this relationship so that a different class/method can be called instead of the one corresponding to the URL.

CodeIgniter provide feature to write your own routing criteria.

Routing rules are defined in your application/config/routes.php file. In it you’ll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions.

CodeIgniter Rocks

Now let’s come to our main point. i.e., How to set CodeIgniter route using HTTP verbs GET,PUT,POST,DELETE,PATCH request for REST API.

How to set CodeIgniter route using HTTP verbs GET,PUT,POST,DELETE,PATCH request for REST API

For set CodeIgniter route using HTTP verbs GET,PUT,POST,DELETE,PATCH request for REST API, $route that permits you to specify your own routing criteria. $route can be multi-dimentional and child array key is for HTTP verbs GET,PUT,POST,DELETE,PATCH or any custom method you like to use.

CodeIgniter: Using HTTP verbs in routes

It is possible to use HTTP verbs (request method) to define your routing rules. This is particularly useful when building RESTful applications. You can use standard HTTP verbs (GET, PUT, POST, DELETE, PATCH) or a custom one such (e.g. PURGE). HTTP verb rules are case-insensitive. All you need to do is to add the verb as an array key to your route. Example:


$route['products']['PUT'] = 'product/update';

In the above example, a PUT request to URI “products” would call the Product::update() controller method.


$route['products']['POST'] = 'product/insert';

In the above example, a POST request to URI “products” would call the Product::insert() controller method.


$route['products']['GET'] = 'product/list';

In the above example, a GET request to URI “products” would call the Product::list() controller method.


$route['products/(:num)']['DELETE'] = 'product/delete/$1';

A DELETE request to URL with “products” as first the segment and a number in the second will be mapped to the Product::delete() method, passing the numeric value as the first parameter.

Using HTTP verbs is of course, optional.

CodeIgniter have many other options too for URI Routing, If you like to check it out visit URI Routing document page.