-
-
Save mariano-aguero/b4e0f4188d8f3063af5e to your computer and use it in GitHub Desktop.
Playing with Laravel Lumen: simple RESTful trait.
This file contains 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; | |
trait RestControllerTrait { | |
/** | |
* Manage index request | |
* @param Request $request | |
* @return type | |
*/ | |
public function index(Request $request) { | |
$m = self::MODEL; | |
//Paginate settings | |
$limit = $request->get('limit', 10); | |
$page = $request->get('page', 1); | |
\Illuminate\Pagination\Paginator::currentPageResolver(function() use ($page) { | |
return $page; | |
}); | |
$data = $m::paginate($limit); | |
return $this->listResponse($data); | |
} | |
/** | |
* Manage show profile request | |
* @param integer $id | |
* @return type | |
*/ | |
public function show($id) { | |
$m = self::MODEL; | |
if ($data = $m::find($id)) { | |
return $this->showResponse($data); | |
} | |
return $this->notFoundResponse(); | |
} | |
/** | |
* Manage store request | |
* @param Request $request | |
* @return type | |
* @throws \Exception | |
*/ | |
public function store(Request $request) { | |
$m = self::MODEL; | |
try { | |
$v = \Validator::make($request->all(), $this->validationRules); | |
if ($v->fails()) { | |
throw new \Exception("ValidationException"); | |
} | |
$data = $m::create($request->all()); | |
return $this->createdResponse($data); | |
} catch (\Exception $ex) { | |
$data = ['form_validations' => $v->errors(), 'exception' => $ex->getMessage()]; | |
return $this->clientErrorResponse($data); | |
} | |
} | |
/** | |
* Manage update request | |
* @param type $id | |
* @param Request $request | |
* @return type | |
* @throws \Exception | |
*/ | |
public function update(Request $request, $id) { | |
$m = self::MODEL; | |
if (!$data = $m::find($id)) { | |
return $this->notFoundResponse(); | |
} | |
try { | |
$v = \Validator::make($request->all(), $this->validationRules); | |
if ($v->fails()) { | |
throw new \Exception("ValidationException"); | |
} | |
$data->fill($request->all()); | |
$data->save(); | |
return $this->showResponse($data); | |
} catch (\Exception $ex) { | |
$data = ['form_validations' => $v->errors(), 'exception' => $ex->getMessage()]; | |
return $this->clientErrorResponse($data); | |
} | |
} | |
/** | |
* Manage delete request | |
* @param type $id | |
* @return type | |
*/ | |
public function destroy($id) { | |
$m = self::MODEL; | |
if (!$data = $m::find($id)) { | |
return $this->notFoundResponse(); | |
} | |
$data->delete(); | |
return $this->deletedResponse(); | |
} | |
/** | |
* Show json individual response | |
* @param type $data | |
* @return type | |
*/ | |
protected function createdResponse($data) { | |
$response = [ | |
'code' => 201, | |
'status' => 'success', | |
'data' => $data | |
]; | |
return response()->json($response, $response['code']); | |
} | |
/** | |
* Show json individual response | |
* @param type $data | |
* @return type | |
*/ | |
protected function showResponse($data) { | |
$response = [ | |
'code' => 200, | |
'status' => 'success', | |
'data' => $data, | |
]; | |
return response()->json($response, $response['code']); | |
} | |
/** | |
* List json individual response with paginate | |
* @param type $data | |
* @return type | |
*/ | |
protected function listResponse($data) { | |
$response = [ | |
'code' => 200, | |
'status' => 'success', | |
'paginator' => [ | |
'pages' => (int) $data->lastPage(), | |
'current_page' => (int) $data->currentPage(), | |
'per_page' => (int) $data->perPage(), | |
'total' => (int) $data->total(), | |
], | |
'data' => $data->all(), | |
]; | |
return response()->json($response, $response['code']); | |
} | |
/** | |
* Not found response | |
* @return type | |
*/ | |
protected function notFoundResponse() { | |
$response = [ | |
'code' => 404, | |
'status' => 'error', | |
'data' => 'Resource Not Found', | |
'message' => 'Not Found' | |
]; | |
return response()->json($response, $response['code']); | |
} | |
/** | |
* Deleted response | |
* @return type | |
*/ | |
protected function deletedResponse() { | |
$response = [ | |
'code' => 200, | |
'status' => 'success', | |
'data' => 'Resource deleted', | |
'message' => 'Deleted' | |
]; | |
return response()->json($response, $response['code']); | |
} | |
/** | |
* Client error response | |
* @param type $data | |
* @return type | |
*/ | |
protected function clientErrorResponse($data) { | |
$response = [ | |
'code' => 422, | |
'status' => 'error', | |
'data' => $data, | |
'message' => 'Unprocessable entity' | |
]; | |
return response()->json($response, $response['code']); | |
} | |
} |
This file contains 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 | |
/* | |
|-------------------------------------------------------------------------- | |
| Application Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register all of the routes for an application. | |
| It's a breeze. Simply tell Laravel the URIs it should respond to | |
| and give it the controller to call when that URI is requested. | |
| | |
*/ | |
function rest($path, $controller) { | |
global $app; | |
$app->get($path, $controller . '@index'); | |
$app->get($path . '/{id}', $controller . '@show'); | |
$app->post($path, $controller . '@store'); | |
$app->put($path . '/{id}', $controller . '@update'); | |
$app->delete($path . '/{id}', $controller . '@destroy'); | |
} | |
rest('/user', 'App\Http\Controllers\UserController'); | |
$app->get('/', function() use ($app) { | |
return $app->welcome(); | |
}); |
This file contains 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 Laravel\Lumen\Routing\Controller as BaseController; | |
class UserController extends BaseController { | |
use RestControllerTrait; | |
const MODEL = 'App\Models\User'; | |
protected $validationRules = ['username' => 'required', 'name' => 'required', 'password' => 'required']; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment