1. Go to this page and download the library: Download nmirceac/api-tools library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nmirceac / api-tools example snippets
php
namespace App\Http\Controllers\Api;
/**
* Class User
* @apiModel User
* @apiDescription Exposing the Users API interfaces
* @package App\Http\Controllers\Api
*/
class User extends \ApiTools\Http\Controllers\BaseController
{
public $class = \App\User::class;
protected $itemName = 'client';
protected $orderAsc = false;
protected $orderBy = 'created_at';
protected $itemsPerPage = 15;
protected $singleAppends = ['thumbnail'];
protected $multipleAppends = ['thumbnail'];
protected $singleRelationships = ['images'];
protected $multipleRelationships = [];
protected $searchColumns = [];
}
php
// sendReponse
//
// for get method, the response should be wrapped in a sendResponse
/**
* @apiDescription Get age bracket from age
* @param string $age
* @apiExampleParamAge 33
* @return \Illuminate\Http\JsonResponse
*/
public function getAgeBracketFromAge($age)
{
return $this->sendResponse(\App\Calculator::getAgeBracketFromAge($age));
}
// apiExampleReturn
//
// docblock example response
/**
* @apiDescription Get government departments
* @return \Illuminate\Http\JsonResponse
* @apiExampleReturn {"success":true,"data":[{"id":1,"label":"Agriculture, Forestry and Fisheries [ Department of ]"},{"id":2,"label":"Arts and Culture [ Department of ]"},...{"id":49,"label":"Not sure"}]}
*/
public function getGovernmentDepartments()
{
$options = [];
foreach(\App\GovernmentDepartments::pluck('department_name', 'id') as $id=>$label) {
$options[] = ['id'=>$id, 'label'=>$label];
}
return $this->sendResponse($options);
}
// apiExampleParamName
//
// docblock example parameters for documentation
/**
* @apiDescription BMI Calculator
* @param int $height
* @param int $weight
* @apiExampleParamHeight 179
* @apiExampleParamWeight 85
* @apiExampleReturn {"success":true,"data":{"bmi":26.53,"description":"Overweight"}}
* @return \Illuminate\Http\JsonResponse
*/
public function getBmi(int $height, int $weight)
{
return $this->sendResponse(\App\Calculator::calculateBmi($height, $weight));
}
// sendAck
//
// post routes expect a sendAck response - its content is optional
/**
* @apiDescription MyCover calculator
* @apiRequestParamLife_code 34-M-NS-3
* @apiRequestParamLpp 2
* @apiRequestParamBenefactor 1
* @apiRequestParamBeneficiary 3
* @apiRequestParamMinimum_cover 1000000
* $apiExampleReturn {"success":true,"data":{"premium":216,"maxBenefit":1000000,"prinicpleLiquidity":100000,"estateProtection":5000,"spouseLiquidity":30000,"childrenLiquidity":0,"childLiquidity":40000,"totalBenefits":1175000}}
* @return \Illuminate\Http\JsonResponse
*/
public function myCoverCalculator()
{
$lifeCode = request('life_code');
$lpp = request('lpp');
$benefactor = request('benefactor');
$beneficiary = request('beneficiary');
$minimumCover = request('minimum_cover');
$summary = \App\Calculator::getMyCoverCalculations($lifeCode, $lpp, $benefactor, $beneficiary, $minimumCover);
return $this->sendAck($summary);
}
// sendError
//
// this will throw the desired exception to the apiClient
/**
* @apiDescription Get participants for a user
* @param int $id
* @return \Illuminate\Http\JsonResponse
*/
public function getParticipants(int $id)
{
$i = $this->class::find($id);
if(is_null($i)) {
return $this->sendError('User not found', [], 404);
}
return $this->sendResponse($i->participants()->with('submissions')->get());
}
// apiSupportsPagination
//
// auto pagination example - @apiSupportsPagination
// the apiClient published method will automaticall add the current page
//
// apiRequestParamPage
// this will add a request parameter "page" with the value of "1"
// in the documentation example
/**
* @apiDescription Returns upgrades for intermediary
* @param int $id
* @apiExampleId 3
* @apiSupportsPagination
* @apiRequestParamPage 1
* @return \Illuminate\Http\JsonResponse
*/
public function getForIntermediary(int $id)
{
$with = [];
if(request('with')) {
$with = explode(',', request('with', ''));
}
$upgrades = $this->class::query()
->with($with)
->paginate();
return self::sendResponse($upgrades);
}
// apiPostParamPassword auto add post params to published method
//
// the generated api client method will have the following parameters
// setPassword(int $id, $password, $data=[]);
/**
* @apiDescription Set user's password
* @param int $id
* @apiRequestParamPassword testPassword
* @apiPostParamPassword password
* @return \Illuminate\Http\JsonResponse
*/
public function setPassword(int $id)
{
$i = $this->class::with($this->singleRelationships);
$i = $i->find($id);
if (!$i) {
return $this->sendError('Not Found');
}
$i->password = request('password');
$i->save();
return $this->sendAck();
}