Skip to content

Instantly share code, notes, and snippets.

@kjunggithub
Created October 30, 2014 14:09
Show Gist options
  • Save kjunggithub/f253266fa2ecb9c642e0 to your computer and use it in GitHub Desktop.
Save kjunggithub/f253266fa2ecb9c642e0 to your computer and use it in GitHub Desktop.
api api
<?php
class ApiController extends \BaseController {
// extend apicontroller from homecontroller
//
// build functions for 400, 403, 500, and other ones that we may use
// return errors with:
// $this->respondNotFound('blah blah cant be found');
//
// return success with:
// $this->respond(array(
// 'status' => 'success'
// 'message' => 'worked!'
// 'data' => $users
// ));
// list of status codes
// HttpFoundation/Response
// default 200
protected $statusCode = 200;
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
const HTTP_NOT_FOUND = 404;
protected function setStatusCode($statusCode) {
$this->statusCode = $statusCode;
return $this;
}
protected function getStatusCode() {
return $this->statusCode;
}
protected function respondNotFound($message = 'Not found.') {
return $this->setStatusCode(404)->respondError ($message);
}
protected function respondError($message) {
$data = array(
'error' => array(
'message' => $message,
'status_code' => $this->getStatusCode()
)
);
return $this->respond($data);
}
protected function respond($data, $headers = array()) {
return Response::json($data, $this->getStatusCode(), $headers);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment