Skip to content

Instantly share code, notes, and snippets.

@csinghdev
Last active April 12, 2020 13:12
Show Gist options
  • Save csinghdev/84810412ba945395469c8af63bfeafed to your computer and use it in GitHub Desktop.
Save csinghdev/84810412ba945395469c8af63bfeafed to your computer and use it in GitHub Desktop.
Response management functions in Controller
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use MarcinOrlowski\ResponseBuilder\ResponseBuilder;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function respond($data, $msg = null) {
return ResponseBuilder::asSuccess()->withData($data)->withMessage($msg)->build();
}
public function respondWithMessage($msg) {
return ResponseBuilder::asSuccess()->withMessage($msg)->build();
}
public function respondWithError($api_code, $http_code) {
return ResponseBuilder::asError($api_code)->withHttpCode($http_code)->build();
}
public function respondBadRequest($api_code) {
return $this->respondWithError($api_code, 400);
}
public function respondUnAuthorizedRequest($api_code) {
return $this->respondWithError($api_code, 401);
}
public function respondNotFound($api_code) {
return $this->respondWithError($api_code, 404);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment