PhpRestService: Simple PHP5 REST framework
Once again I started another open source project. This project focuses on setting up RESTful webservices with PHP. The REpresentational State Transfer (REST) protocol is a set of architectural principles for designing RESTful webservices. The term representational state transfer was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation.
According this Packtpub arcticle called Resource Oriented Clients with REST principles, the key REST principles include:
- The concept of resource (for example, a document is a resource)
- Every resource given a unique ID (for example, document URL)
- Resources can be related (for example, One document linking to another)
- Use of standard (HTTP, HTML, XML, JSON)
- Resources can have multiple forms (for example, status of a document, updated, validated, deleted)
- Communicate in a stateless fashion using HTTP
Another key aspect of RESTful webservices is using HTTP status codes to communicate the result of a certain operation and negotiate about the representation. Following these principles correctly makes it easy to implement cache and/or a layered architecture.
Before starting a new project I did a bit of research of current REST frameworks available for PHP. First of all there is the Zend Framework with a Zend_Rest_Service, which does not follow all of the REST principles. This class wraps around an existing class aiming to make it RESTful. Another solution from the Zend Framework is the Zend_Rest_Route component. It exists of a skeleton controller with the http methods as actions. My personal problem with this solution is that it does not make a difference between a collection and an item. The Recess framework also contains a solution for creating restful webservices, but it is packaged with a complete framework for setting up web pages.
PhpRestService will be a library for setting up rest services quickly. It will make use of HTTP status codes and headers for communicating the result of an action and selecting the preferred representation of a resource. Many things will be configurable by editing configuration files without touching any code. The project structure is almost identical to the one of PhpTaskDaemon, containing a library and application directory. When using the library, the user creates a set of resources in the application directory by defining, the one of the HTTP methods: get, post, put, delete, head and/or options. A distinction is made between a collection (/blog) and an item resource (/blog/<id>). The development progress of this project can be found on GitHub: https://github.com/DirkEngels/PhpRestService/
The end result will contain two sample applications; the first is a simple blog application, where as the second one displays the status of a PhpTaskDaemon instance. I will look forward to your comments and remarks regarding this project.