Quick and short example application for a PHP-based JSON API with all of the testing, linting, and build info.
- Download this repository
- Install the VM with
vagrant up
- Install dependencies with
composer install
- Test web app at
http://localhost:8080/ping
- PHPUnit tests are located in the
/tests
directory and are automatically run on Composer updates. - PHP Mess Detector and Code Sniffer are also automatically run
- PHPDocumentor has to be manually run with
composer run-script document
Using Travis CI to automatically run the code quality tools on branch changes.
The configuration JSON file is loaded via the \App\Utils\Config singleton class and provides easy getters for complex values.
SlimPHP handles all of the routing. Just one route in the main app.php now for demo purposes.
Using the Eloquent ORM package (from the Laravel project) as a standalone. There is a generic customer database file used to scaffold some data, and I've built two relational models as an example.
The controllers are generic callbacks that are referenced by the Slim PHP route calls
Using JWT authentication. The secret is in the config.json, but you should NEVER version or make this value public. Only here for demonstration purposes.
All endpoints require authentication except for the /ping
endpoint and the /token
endpoint which is used to generate a new token for a user.
For the purposes of this demo, you can use this token:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJTbGltIFBIUCBEZW1vIEFwcCIsImlhdCI6MTQ5ODc4MjAyNSwiZXhwIjoxNTYxODU0MDI1LCJhdWQiOiJ3d3cuc2xpbS1waHAtZGVtby5jb20iLCJzdWIiOiJ0ZXN0QGV4YW1wbGUuY29tIiwiR2l2ZW5OYW1lIjoiSmFuZSIsIlN1cm5hbWUiOiJEb2UiLCJFbWFpbCI6ImphbmUuZG9lQGV4YW1wbGUuY29tIiwiUm9sZSI6IkFkbWluIn0.aukn-FcigKdQ_kOFviMGuj1D5CxrjBWEt37yykuAdzA
A successful request would look like:
http://localhost:8080/customers 'Cookie:token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJTbGltIFBIUCBEZW1vIEFwcCIsImlhdCI6MTQ5ODc4MjAyNSwiZXhwIjoxNTYxODU0MDI1LCJhdWQiOiJ3d3cuc2xpbS1waHAtZGVtby5jb20iLCJzdWIiOiJ0ZXN0QGV4YW1wbGUuY29tIiwiR2l2ZW5OYW1lIjoiSmFuZSIsIlN1cm5hbWUiOiJEb2UiLCJFbWFpbCI6ImphbmUuZG9lQGV4YW1wbGUuY29tIiwiUm9sZSI6IkFkbWluIn0.aukn-FcigKdQ_kOFviMGuj1D5CxrjBWEt37yykuAdzA'
Always use appropriate status codes (401, 405, etc) for error responses and include a message
property in the JSON response.