This is an example to showcase the creation of a basic REST API using the Zend Framework 2. To install, follow these steps:
- Clone the git repository in your PHP server.
- Install Composer and make sure your PHP server has the sqlite extension loaded.
- Go to the application's root and execute
php composer.phar install
. - Copy the SQLite database
/module/RestApi/data/db/restapi.clean.db
torestapi.db
in the same folder. Make sure both the database and the containing folder have write permission for the web server. - Install [phpunit](https://phpunit.de/ if you want to run the automated tests).
A demo interface has been provided to test the REST API. Open the application's root path in your web browser and feel free to try the different options available.
- This is my very first project with the Zend Framework; I paid attention to follow the recommended best practices, but I have still a lot to learn!
- I couldn't make the framework load a
global|local.php
file in the/module/RestApi/config
folder; I searched on the internet for a while but did not found the issue. - Not implemented HTTP verbs will produce a 405 error code.
- The authentication protocol I implemented is very simplistic: the Token header as it is used by the Ruby on Rails framework.
- The POST return format follows the Ember.js style guides, by putting the data under
customers
orcustomer
; this allows for additional metadata information without interferring with the actual data. - The DELETE action returns a
204 No Content
response, since no information is required from this action; search, however, still returns a200
code with an emptycustomers
array, to allow for further metadata to be returned too. - Special attention has been paid to returning the right HTTP codes, like 200, 204, 400, 404, depending on the case.
- To validate the POST and PUT input when creating and updating customers, I had to make the data go through a Form class so that it would get filtered and validated. This feels rather cumbersome to me, and although I didn't find a reason to do it this way, I kept it since it is the recommended method according to the Zend documentation.
- Models have a complete set of unit tests to showcase the feature; to run them, go to
/module/RestApi/test
and typephpunit
. Behavior tests are missing for controllers - I plan to use Behat, which has a plugin to connect to Zend developed by the community. - The demo interface to demonstrate the CRUD functions is based on a skeleton application provided by Zend; I did not clean all the code, just modified what was needed to get it running.
- I normally would use Sass or Less, but in this case the CSS code was pretty simple so... keeping it simple I am!
- Finally, the JSON responses contains just data; some metadata would be useful, like
count
,page
,pageSize
for collections, and_links
for resources (like in the HAL Model)