8

I need to create a RESTful web api using only CodeIgniter. I can not use any third-party plugins or libraries to do this. I have seen that most people are using https://github.com/chriskacerguis/codeigniter-restserver. Please guide me on writing a REST api only using CodeIgniter. Helpful links and steps are highly appreciated.

Thanks in Advance.

4
  • 1
    @Kisaragi Check Step 1 : "Firstly you need to download the codeigniter-restserver code from GitHub" :( Commented May 4, 2016 at 13:41
  • 1
    Yes, a few new classes added to your CI app. There's nothing 'outside' of codeigniter in there. Either write the exact same thing yourself, or include the library. Commented May 4, 2016 at 14:00
  • This is a stand alone tutorial outergalactic.org/blog/… Commented May 4, 2016 at 21:28
  • Follow this tutorial, but notice that it is made with CI 2. So you need to consider differences between CI v2 and CI v3 (i.e. certain filenames ucfirst, etc). Commented May 4, 2016 at 22:54

1 Answer 1

8

If your are using version 3, you can do this

create a controller users.php

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";
    }

}

edit (add) in your application/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;
};
Sign up to request clarification or add additional context in comments.

3 Comments

application/config/config.php or application/config/routes.php It should be routes.php
sorry, is in application/config/routes.php. Can you sendme the error above, please
This is working, but the problem is, even though we have define the HTTP verb here as like get , post and etc, every url work for every request. That mean users/store url is working for all http methods (GET , POST , PUT , ....)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.