1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| <?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Facades\Response;
class Controller extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; protected $statusCode=200;
public function getStatusCode(){ return $this -> statusCode; }
public function setStatusCode($statusCode){ $this -> statusCode = $statusCode; return $this; }
public function success($data = []){ return $this -> setStatusCode(200) -> responseSuccess($data); }
public function successMsg($msg = 'success'){ return $this -> setStatusCode(200) -> responseSuccessMsg($msg); }
public function warning($msg = 'warning',$msg_type = 'message',$level = 'info'){ return $this -> setStatusCode(205) -> responseWarning($msg,$msg_type,$level); }
public function failed($msg = 'failed'){ return $this -> setStatusCode(400) -> responseError($msg); }
public function notFound($msg = 'not found'){ return $this -> setStatusCode(404) -> responseError($msg); }
public function created($data){ return $this -> setStatusCode(201) -> responseSuccess($data); }
public function exists($msg = 'data already exists'){ return $this -> setStatusCode(202) -> responseWarning($msg); }
public function badRequest($msg = 'Request Parameter Error!'){ return $this -> setStatusCode(422) -> responseError($msg); }
public function unauthorized($msg = 'Unauthorized requests'){ return $this -> setStatusCode(401) -> responseError($msg); }
public function forbidden($msg = 'Insufficient authority'){ return $this -> setStatusCode(403) -> responseError($msg); }
public function methodNotAllowed($msg = 'This request is disabled'){ return $this -> setStatusCode(405) -> responseError($msg); }
public function serverError($msg = 'server error!'){ return $this -> setStatusCode(500) -> responseError($msg); }
protected function responseError($msg) { return $this -> response([ 'result' => 'FAILED', 'status_code' => $this -> getStatusCode(), 'msg' => $msg ])->setStatusCode($this -> getStatusCode()); }
protected function responseSuccess($data = []){ return $this -> response([ 'result' => 'SUCCESS', 'status_code' => $this -> getStatusCode(), 'data' => $data ])->setStatusCode($this -> getStatusCode()); }
public function responseWarning($msg,$msg_type,$level){ return $this -> response([ 'result' => 'WARNING', 'status_code' => $this -> getStatusCode(), 'msg' => $msg, 'msg_type' => $msg_type, 'level' => $level ])->setStatusCode($this -> getStatusCode()); }
public function responseSuccessMsg($msg){ return $this -> response([ 'result' => 'SUCCESS', 'status_code' => $this -> getStatusCode(), 'msg' => $msg ])->setStatusCode($this -> getStatusCode()); }
public function response($data){ return Response::json($data,$this -> getStatusCode()); }
function paginate($data,$perPage,$current_page){ $allItem = $perPage * 100; global $total; if($total > $allItem){ $total = $allItem; }else{ $total = count($data); } $item =array_slice($data,($current_page-1)*$perPage,$perPage); return new LengthAwarePaginator($item,$total,$perPage,$current_page,[ 'path'=> \Illuminate\Pagination\Paginator::resolveCurrentPath(), 'pageName'=>'page' ]); } }
|