Creating a RESTful API using pure CodeIgniter?

I need to create a RESTful web api using only CodeIgniter. I cannot use third-party plugins or libraries for this. I saw that most people use https://github.com/chriskacerguis/codeigniter-restserver . Please direct me to write REST api only with CodeIgniter. Useful links and steps are highly appreciated.

Thanks at Advance.

+4
source share
1 answer

If you are using version 3, you can do it

create users.php controller

class Users extends CI_Controller {

    /**
    * @route http://proyect/users
    * @verb GET
    */
    public function get()
    {
        echo "Get";
    }

    /**
    * @route http://proyect/users
    * @verb POST
    */
    public function store()
    {
        echo "Add";
    }

    /**
    * @route http://proyect/users
    * @verb PUT
    */
    public function update()
    {
        echo "Update";
    }

    /**
    * @route http://proyect/users
    * @verb DELETE
    */
    public function delete()
    {
        echo "Delete";
    }

}

change (add) to you rapplication / config / route.php

$route["users"]["get"]    = "users/get";
$route["users"]["post"]   = "users/store";
$route["users"]["update"] = "users/update";
$route["users"]["delete"] = "users/delete";

$route['products/([a-zA-Z]+)/edit/(\d+)'] = function ($product_type, $id)
{
        return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
};
+6
source

All Articles