Codeigniter - Creating a RESTful API

10,590

It looks like CodeIgniter 2.0 was released in January 2011, nearly a year afterwards, so it may not work. However, CodeIgniter has always been quite strong on backward compatibility, so you may be able to use it without issues.

That said, CodeIgniter is IMHO not the best choice for building a REST API (I've done it myself and I wouldn't recommend it), and it is falling further and further behind some of the more modern PHP frameworks. If it's at all possible or practical to switch at this stage I would use a different framework that's better suited to building API's - if you wanted a full-stack framework that's easy to learn if you know CodeIgniter, Laravel is a good choice, or for something more lightweight, try Slim.

EDIT: Based on the requirements you mentioned in your comment, I would be happy to recommend Laravel over Slim for this use case.

Most server-side frameworks can be sorted into two types:

  • MVC frameworks (eg Rails, Django, CodeIgniter)
  • REST microframeworks (eg Sinatra, Slim, Flask, Express)

Laravel is the first framework I've seen that effectively bridges the gap between these two types. It's also a relatively easy switch if you already have experience with CodeIgniter, has a wonderful ORM, makes unit testing easy, and is built on top of Composer, making it easy to keep it up to date.

Tutorial wise, I can point you at this list of NetTuts tutorials (as long as you stick with ones that cover Laravel 4 you should be fine), and the official documentation, which is extremely good.

Share:
10,590
ScottOBot
Author by

ScottOBot

Updated on June 27, 2022

Comments

  • ScottOBot
    ScottOBot almost 2 years

    Hey so I have been trying to create a RESTful API using codeigniter 2.1.4.

    I am an intermediate php programmer and originally debated created the API from scratch.
    However, after some research, looking through old questions here and on google I picked up a couple tutorials and third party libraries.
    One of the easiest to follow I found at nettuts+.
    This looked like a great solution as it used the philsturgeon / codeigniter-restserver third party libraries but the tutorial its self was written in 2010.

    Upon further inspection I realized that the majority of the tutorials using these libraries were at least 2-3 years old. Will this cuase any issues?
    Can I still follow the tutorial at nettuts+ Or should I just write my own RESTful API?

  • ScottOBot
    ScottOBot about 10 years
    Hey Matthew thanks for the comment. I had suspected after taking a quick glance at the nettuts+ tutorial that there was probably an easier way to implement a RESTful API than the method outlined with codeigniter. I have four requirements for the PHP framework I am going to use for this project, it must be lightweight enough to be installed on a standard Host Gator shared server, it must be easy to install, it must have some support (built in or extendable via third party libraries) for a RESTful API and it must allow for me to create a website with dynamic content loaded from a database.
  • ScottOBot
    ScottOBot about 10 years
    I may also add that his all has to happen on the same server. The front end website is to be hosted on the same server as the RESTful API. I like the looks of Laravel, does it satisfy the above requirements? If the answer is yes, I would love it if you could post a link to a tutorial or guide on installation and getting started with the framework, and perhaps even one on creating a RESTful API. With that done I would be more then happy to select your answer as the correct one. Thanks for the help!
  • Matthew Daly
    Matthew Daly about 10 years
    I'd already added some anyway. NetTuts are quite keen on Laravel so they're a good place to look (just make sure they cover Laravel 4), and the documentation for Laravel is excellent.
  • quantme
    quantme about 9 years
    How was your project @ScottOBot? I'm trying to develop an API and after fighting with Laravel 5 to do Requests/Responses (too complex for me to try L4 tutorials with L5) and I returned to CodeIgniter 2. It has an integrated library. Most of the CI 2 tutorials still work and I've not noticed yet a big difference with the same library in CI 3 documentation. What framework do you recommend?
  • Matthew Daly
    Matthew Daly about 9 years
    @quantme If Laravel 5 is too complex for your use case, have you considered something like Slim or Silex? They add very little overhead, and you're free to use whatever database interface you want, be it MySQLi or PDO, or an ORM or query builder, or even a non-relational database like MongoDB.
  • Felipe Alarcon
    Felipe Alarcon over 8 years
    I read somewhere in the laravel forums that it doesn't work on a shared hosting, have you been able to implement it?