Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter - Creating a RESTful API

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?

like image 841
ScottOBot Avatar asked Feb 02 '14 13:02

ScottOBot


People also ask

Can we write REST API in PHP?

REST API provides endpoints (URLs) that you call to perform CRUD operations with your database on the server. Previously, we have talked about How to build a REST API in Node. js and Flask. Today, we will learn how to set up a PHP server and write a small REST API using it.

What is JWT token in CodeIgniter?

JWT stands for JSON Web Token, it is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. JWT is commonly used for Authorization , Information Exchange and etc.

What can I use CodeIgniter for?

CodeIgniter is a PHP MVC framework used for developing web applications rapidly. CodeIgniter provides out of the box libraries for connecting to the database and performing various operations like sending emails, uploading files, managing sessions, etc.


1 Answers

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.

like image 155
Matthew Daly Avatar answered Sep 28 '22 00:09

Matthew Daly