mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2024-12-27 22:45:27 +03:00
942b8fefb4
Using PSR-2 standard.
60 lines
1.4 KiB
Plaintext
60 lines
1.4 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);
|
|
}
|
|
|
|
}
|