Skip to content

Example of a PHP API with the Eloquent ORM and other features

Notifications You must be signed in to change notification settings

markparolisi/slim-api

Repository files navigation

DEMO Slim PHP API

Build Status

Quick and short example application for a PHP-based JSON API with all of the testing, linting, and build info.

Installation

  1. Download this repository
  2. Install the VM with vagrant up
  3. Install dependencies with composer install
  4. Test web app at http://localhost:8080/ping

Code Quality

Continuous Integration

Using Travis CI to automatically run the code quality tools on branch changes.

Config

The configuration JSON file is loaded via the \App\Utils\Config singleton class and provides easy getters for complex values.

Routing

SlimPHP handles all of the routing. Just one route in the main app.php now for demo purposes.

Models

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.

Controllers

The controllers are generic callbacks that are referenced by the Slim PHP route calls

Authentication

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'

Error Messaging

Always use appropriate status codes (401, 405, etc) for error responses and include a message property in the JSON response.

About

Example of a PHP API with the Eloquent ORM and other features

Resources

Stars

Watchers

Forks

Packages

No packages published