Skip to content

Instantly share code, notes, and snippets.

@smgladkovskiy
Created November 7, 2014 07:51
Show Gist options
  • Save smgladkovskiy/db04830fcfa9129f26c0 to your computer and use it in GitHub Desktop.
Save smgladkovskiy/db04830fcfa9129f26c0 to your computer and use it in GitHub Desktop.
Laravel 4 Ajax Base Controller
<?php namespace Ajax;
use Controller;
use Illuminate\Http\Response;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\MessageBag;
use Laracasts\Commander\CommanderTrait;
use Response as RequestResponse;
class BaseController extends Controller {
use CommanderTrait;
/**
* @var int
*/
private $statusCode = 200;
/**
* @var string
*/
private $redirectUrl;
/**
* @var array
*/
private $refreshData = [];
/**
* @var array
*/
private $data = null;
/**
* @var
*/
private $pagination;
/**
* @param string $message
* @param null $section
*
* @return \Illuminate\Http\JsonResponse
*/
public function respondNotFound($message = 'Not found', $section = null)
{
return $this->setStatusCode(Response::HTTP_NOT_FOUND)
->respondWithError($message, $section);
}
/**
* @param $message
* @param null $section
*
* @return \Illuminate\Http\JsonResponse
*/
public function respondWithError($message, $section = null)
{
return $this->respond([
'message' => $this->getMessage('error', $message, $section)
]);
}
/**
* @param array $data
* @param array $headers
*
* @return \Illuminate\Http\JsonResponse
*/
public function respond($data = [], $headers = [])
{
$data += ['status' => $this->getStatusCode()];
if($this->getRedirectUrl())
{
$data += ['redirect' => $this->getRedirectUrl()];
}
if($this->getRefreshData())
{
$data += ['refresh' => $this->getRefreshData()];
}
if($this->getData() !== null)
{
$data += ['data' => $this->getData()];
}
if($this->getPagination())
{
$data += ['pagination' => $this->getPagination()];
}
$headers += [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET, POST'
];
return RequestResponse::json($data, $this->getStatusCode(), $headers,
JSON_UNESCAPED_UNICODE);
}
/**
* @return mixed
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* @param mixed $statusCode
*
* @return $this
*/
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
/**
* @return mixed
*/
public function getRedirectUrl()
{
return $this->redirectUrl;
}
/**
* @param mixed $redirectUrl
*
* @return $this
*/
public function setRedirectUrl($redirectUrl)
{
$this->redirectUrl = $redirectUrl;
return $this;
}
/**
* @return array
*/
public function getRefreshData()
{
return $this->refreshData;
}
/**
* @param $array
*
* @return $this
*/
public function setRefreshData($array)
{
$this->refreshData = $array;
return $this;
}
/**
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* @param array $data
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* @return mixed
*/
public function getPagination()
{
return $this->pagination;
}
/**
* @param Paginator $object
* @param bool $pagination_view
*
* @return $this
*/
public function setPagination(Paginator $object, $pagination_view = false)
{
$total_pages = ceil($object->getTotal() / $object->getPerPage());
$next_page = ($object->getCurrentPage() == $object->getLastPage())
? null : $object->getCurrentPage() + 1;
$previous_page = ($object->getCurrentPage() == 1)
? null : $object->getCurrentPage() - 1;
$this->pagination = ($pagination_view)
? $object->links()->render()
: [
'total_items' => $object->getTotal(),
'total_pages' => $total_pages,
'per_page' => $object->getPerPage(),
'current_page' => $object->getCurrentPage(),
'previous_page' => $previous_page,
'next_page' => $next_page,
'last_page' => $object->getLastPage(),
];
return $this;
}
/**
* @param $type
* @param $message
* @param null $section
*
* @return array
*/
private function getMessage($type, $message, $section = null)
{
$messages = [];
if(is_array($message))
{
foreach($message as $_section => $error)
{
if(is_string($_section))
{
$section = $_section;
}
$messages[] = $this->prepareMessage($type, $error, $section);
}
}
elseif($message instanceof MessageBag)
{
foreach($message->toArray() as $_section => $_messages)
{
foreach($_messages as $_message)
{
$messages[] =
$this->prepareMessage($type, $_message, $_section);
}
}
}
else
{
$messages[] = $this->prepareMessage($type, $message, $section);
}
return $messages;
}
/**
* @param $type
* @param $message
* @param $section
*
* @return array
*/
private function prepareMessage($type, $message, $section)
{
return [
'type' => $type,
'text' => $message,
'status_code' => $this->getStatusCode(),
'section' => $section,
];
}
/**
* @param $message
*
* @return \Illuminate\Http\JsonResponse
*/
public function respondForbidden($message = 'Forbidden')
{
return $this->setStatusCode(Response::HTTP_FORBIDDEN)
->respondWithError($message);
}
/**
* @param $message
*
* @return \Illuminate\Http\JsonResponse
*/
public function respondUnauthorized($message = 'Unauthorized')
{
return $this->setStatusCode(Response::HTTP_UNAUTHORIZED)
->respondWithError($message);
}
/**
* @param $message
*
* @return \Illuminate\Http\JsonResponse
*/
public function respondInternalError($message = 'Internal Server Error')
{
return $this->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR)
->respondWithError($message);
}
/**
* @param $message
*/
public function respondWithSuccess($message)
{
return $this->respond([
'message' => $this->getMessage('success', $message),
]);
}
/**
* @param string $message
* @param null $section
*
* @return \Illuminate\Http\JsonResponse
*/
public function respondWithBadRequest(
$message = 'Bad Request',
$section = null)
{
return $this->setStatusCode(Response::HTTP_BAD_REQUEST)
->respondWithError($message, $section);
}
/**
* @param Paginator $object
* @param $data
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithPagination(
Paginator $object,
$pagination_view = false)
{
return $this->setPagination($object, $pagination_view)->respond();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment