lumen-generators/templates/controller/rest-actions.wnt
AsafMah f954b5481e Standardize response codes.
Lumen already has built in response codes, using them will make the code less error prone.
2015-10-08 14:34:06 +03:00

60 lines
1.2 KiB
Plaintext

<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
trait RESTActions {
public function all()
{
$m = self::MODEL;
return $this->respond(Response::HTTP_OK, $m::all());
}
public function get($id)
{
$m = self::MODEL;
$model = $m::find($id);
if(is_null($model)){
return $this->respond(Response::HTTP_NOT_FOUND);
}
return $this->respond(Response::HTTP_OK, $model);
}
public function add(Request $request)
{
$m = self::MODEL;
$this->validate($request, $m::$rules);
return $this->respond(Response::HTTP_CREATED, $m::create($request->all()));
}
public function put(Request $request, $id)
{
$m = self::MODEL;
$this->validate($request, $m::$rules);
$model = $m::find($id);
if(is_null($model)){
return $this->respond(Response::HTTP_NOT_FOUND);
}
$model->update($request->all());
return $this->respond(Response::HTTP_OK, $model);
}
public function remove($id)
{
$m = self::MODEL;
if(is_null($m::find($id))){
return $this->respond(Response::HTTP_NOT_FOUND);
}
$m::destroy($id);
return $this->respond(Response::HTTP_NO_CONTENT);
}
protected function respond($status, $data = [])
{
return response()->json($data, $status);
}
}