项目作者: GhazanfarMir

项目描述 :
Slim3 + Eloquent
高级语言: PHP
项目地址: git://github.com/GhazanfarMir/Slim3-Rest-Api.git
创建时间: 2018-01-09T16:30:39Z
项目社区:https://github.com/GhazanfarMir/Slim3-Rest-Api

开源协议:MIT License

下载


Slim3 Framework REST Api with CRUD

StyleCI
Build Status

This is the simple REST Api implementing basic CRUD operations related to a user.

Installation

Git Clone

To get the latest source you can use git clone.

$ git clone https://github.com/ghazanfarmir/Slim-RestApi-CRUD.git /path/to/slim-rest-api-crud

Composer

Installation can be done with the use of composer. If you don’t have composer yet you can install it by doing:

$ curl -s https://getcomposer.org/installer | php

To install it globally

$ sudo mv composer.phar /usr/local/bin/composer

  1. $ cd /path/to/slim-rest-api-crud
  2. $ composer update
  3. $ composer install

Database Setup

  • Import dababase/mysql.sql in your MySQL Server, this will create users table.
  • Copy .env.example to .env
  • Update .env with your database credentials
  1. // .env
  2. # Database
  3. DB_DRIVER=mysql
  4. DB_HOST=localhost
  5. DB_NAME=
  6. DB_USERNAME=
  7. DB_PASSWORD=

Usage

Api Endpoints

  1. $ GET /api/v1/users // all users
  2. $ GET /api/v1/users/{id} // specific user
  3. $ POST /api/v1/users // add new user
  4. $ PUT /api/v1/users/{id} // update an existing user
  5. $ DELETE /api/v1/users/{id} // delete an existing user

Requests (POST/PUT)

$ POST /api/v1/users

  1. {
  2. "first_name": "John",
  3. "surname": "Doe",
  4. "email": "johndoe@example.com",
  5. "date_of_birth": "1920-03-02"
  6. }

$ PUT /api/v1/users/{id}

  1. {
  2. "first_name": "John",
  3. "surname": "Doe",
  4. "email": "johndoe@example.com",
  5. "date_of_birth": "1920-03-02"
  6. }

Responses

Each endpoint provides an JSON response with relevant data

$ GET /api/v1/users

  1. {
  2. 'code': 200,
  3. 'total_results': {total_items_count}
  4. 'data': [
  5. { ... },
  6. { ... },
  7. { ... },
  8. ...
  9. ]
  10. }

$ GET /api/v1/users/{id}

  1. {
  2. 'code': 200,
  3. 'data': {
  4. ...
  5. ...
  6. }
  7. }
  8. OR
  9. {
  10. 'code': 404,
  11. 'message': 'no user found'
  12. }

$ POST /api/v1/users

  1. {
  2. 'code': 201,
  3. 'message': 'New user added successfully.',
  4. 'data': {
  5. ...
  6. ...
  7. }
  8. }

$ PUT /api/v1/users/{id}

  1. {
  2. 'code': 200,
  3. 'message': 'User has been updated successfully.',
  4. 'data': {
  5. ...
  6. ...
  7. }
  8. }

$ DELETE /api/v1/users/{id}

  1. {
  2. 'code': 200,
  3. 'message': 'User has been deleted successfully.'
  4. }