Last active
February 6, 2023 11:35
-
-
Save Idnan/a7371740a0ae4a6de152 to your computer and use it in GitHub Desktop.
Laravel Lumen: RESTful trait
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; | |
trait RestControllerTrait { | |
public function index() { | |
$model = self::MODEL; | |
return $this->successResponse($model::all()); | |
} | |
public function show($id) { | |
$model = self::MODEL; | |
if ($data = $model::find($id)) { | |
return $this->successResponse($data); | |
} | |
return $this->notFoundResponse(); | |
} | |
public function store(Request $request) { | |
$model = self::MODEL; | |
try { | |
$v = \Validator::make($request->all(), $this->validationRules); | |
if ($v->fails()) { | |
throw new \Exception('ValidationException'); | |
} | |
$data = $model::create($request->all()); | |
return $this->successResponse($data); | |
} catch (\Exception $exception) { | |
$data = ['errors' => $v->errors(), 'exception' => $exception->getMessage()]; | |
return $this->errorResponse($data); | |
} | |
} | |
public function update(Request $request, $id) { | |
$model = self::MODEL; | |
if(!$data = $model::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->successResponse($data); | |
} catch(\Exception $exception) { | |
$data = ['errors' => $v->errors(), 'exception' => $exception->getMessage()]; | |
return $this->errorResponse($data); | |
} | |
} | |
public function destroy($id) { | |
$model = self::MODEL; | |
if (!$data = $model::find($id)) { | |
return $this->notFoundResponse(); | |
} | |
$data->delete(); | |
return $this->deleteResponse(); | |
} | |
protected function successResponse($data) { | |
$response = [ | |
'code' => 200, | |
'status' => 'success', | |
'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']); | |
} | |
public function deleteResponse() { | |
$response = [ | |
'code' => 204, | |
'status' => 'success', | |
'data' => [], | |
'message' => 'Resource Deleted' | |
]; | |
return response()->json($response, $response['code']); | |
} | |
public function errorResponse($data) { | |
$response = [ | |
'code' => 422, | |
'status' => 'error', | |
'data' => $data, | |
'message' => 'Unprocessable Entity' | |
]; | |
return response()->json($response, $response['code']); | |
} | |
} |
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 Laravel\Lumen\Routing\Controller as BaseController; | |
class BookController extends BaseController { | |
use RestControllerTrait; | |
const MODEL = 'App\Models\Book'; | |
protected $validationRules = [ | |
'title' => 'required', | |
'author' => 'required', | |
'isbn' => 'required' | |
]; | |
} |
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 | |
/* | |
|-------------------------------------------------------------------------- | |
| Application Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register all of the routes for an application. | |
| It is a breeze. Simply tell Lumen the URIs it should respond to | |
| and give it the Closure to call when that URI is requested. | |
| | |
*/ | |
function rest($path, $controller, $groupPrefix = 'api/v1') { | |
global $app; | |
$app->group(['prefix' => $groupPrefix], function() use ($app, $path, $controller) { | |
$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('/book', '\App\Http\Controllers\BookController'); | |
$app->get('/', function() use ($app) { | |
return $app->version(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment