Created
August 17, 2015 16:07
-
-
Save bitkill/6204c123cc21abf985a3 to your computer and use it in GitHub Desktop.
Lumen Rest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ConfigController { | |
const MODEL = 'App\Models\Config'; | |
// see RestControllerTrait. sends less data on indexing the list of the model | |
// protected $indexFields = [ 'name' ]; | |
protected $triggers = [ | |
// 'show' => 'extendObject', | |
// 'update' => 'dataFormatFix', | |
// 'store' => 'dataFormatFix' | |
]; | |
protected $validationRules = [ | |
'name' => 'required', | |
'running_hours' => 'required', | |
'more_info' => 'required', | |
]; | |
use ResponsesTrait, RestControllerTrait; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Date: 24/07/15 | |
* Time: 19:45 | |
*/ | |
namespace App\Http\Controllers; | |
trait ResponsesTrait { | |
protected function call_trigger($trigger, &$data) { | |
if (isset($this->triggers) && isset($this->triggers[$trigger])) { | |
$this->{$this->triggers[$trigger]}($data); | |
} | |
} | |
protected function createdResponse($data) | |
{ | |
$response = [ | |
'code' => 201, | |
'status' => 'succcess', | |
'data' => $data | |
]; | |
return response()->json($response, $response['code']); | |
} | |
protected function showResponse($data) | |
{ | |
$response = [ | |
'code' => 200, | |
'status' => 'succcess', | |
'data' => $data | |
]; | |
return response()->json($response, $response['code']); | |
} | |
protected function listResponse($data) | |
{ | |
$response = [ | |
'code' => 200, | |
'status' => 'succcess', | |
'data' => $data | |
]; | |
return response()->json($response, $response['code']); | |
} | |
protected function notFoundResponse() | |
{ | |
$response = [ | |
'code' => 404, | |
'status' => 'error', | |
'data' => 'Resource Not Found', | |
'message' => 'Not Found' | |
]; | |
return response()->json($response, $response['code']); | |
} | |
protected function deletedResponse() | |
{ | |
$data = []; | |
$response = [ | |
'code' => 204, | |
'status' => 'success', | |
'data' => $data, | |
'message' => 'Resource deleted' | |
]; | |
return response()->json($response, $response['code']); | |
} | |
protected function clientErrorResponse($data) | |
{ | |
$response = [ | |
'code' => 422, | |
'status' => 'error', | |
'data' => $data, | |
'message' => 'Unprocessable entity' | |
]; | |
return response()->json($response, $response['code']); | |
} | |
protected function fileResponse(&$data, $mime_type, $name = null) { | |
if ($name == null) $name = (new \DateTime())->getTimestamp(); | |
return response()->make($data, 200, [ | |
'Content-Type' => $mime_type, | |
'Content-Length' => sizeof($data), | |
'Content-Disposition' => 'inline; '. $name, | |
]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Validator; | |
trait RestControllerTrait | |
{ | |
//use ResponsesTrait; | |
abstract protected function createdResponse($data); | |
abstract protected function showResponse($data); | |
abstract protected function listResponse($data); | |
abstract protected function notFoundResponse(); | |
abstract protected function deletedResponse(); | |
abstract protected function clientErrorResponse($data); | |
abstract protected function call_trigger($trigger, &$data); | |
public function index() { | |
$m = self::MODEL; | |
$data = isset($this->indexFields) ? $m::all($this->indexFields) : $m::all(); | |
$this->call_trigger('index', $data); | |
return $this->listResponse($data); | |
} | |
public function show($id) { | |
$m = self::MODEL; | |
if($data = $m::find($id)) { | |
$this->call_trigger('show', $data); | |
return $this->showResponse($data); | |
} | |
return $this->notFoundResponse(); | |
} | |
public function store(Request $request) { | |
$m = self::MODEL; | |
try { | |
$v = Validator::make($request->all(), $this->validationRules); | |
if($v->fails()) { | |
throw new \Exception("ValidationException"); | |
} | |
$req = \Request::all(); | |
$this->call_trigger('store', $req); | |
$data = $m::create($req); | |
$this->call_trigger('after_store', $data); | |
return $this->createdResponse($data); | |
} catch(\Exception $ex) { | |
$data = ['form_validations' => $v->errors(), 'exception' => $ex->getMessage()]; | |
return $this->clientErrorResponse($data); | |
} | |
} | |
public function update($id) | |
{ | |
$m = self::MODEL; | |
if(!$data = $m::find($id)) { | |
return $this->notFoundResponse(); | |
} | |
$v = \Validator::make(\Request::all(), $this->validationRules); | |
try { | |
if($v->fails()) { | |
throw new \Exception("ValidationException"); | |
} | |
$req = \Request::all(); | |
$this->call_trigger('update', $req); | |
$data->fill($req); | |
$data->save(); | |
$this->call_trigger('after_update', $data); | |
return $this->showResponse($data); | |
} catch(\Exception $ex) { | |
$data = ['form_validations' => $v->errors(), 'exception' => $ex->getMessage()]; | |
return $this->clientErrorResponse($data); | |
} | |
} | |
public function destroy($id) | |
{ | |
$m = self::MODEL; | |
if(!$data = $m::find($id)) | |
{ | |
return $this->notFoundResponse(); | |
} | |
$data->delete(); | |
return $this->deletedResponse(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment