Skip to content
On this page

Api Base Controller

A ApiController is a Base class that acts as a foundation or blueprint for other controllers in the application that is created for Api. It provides a convenient way to define common functionality that can be shared across multiple controllers.

A ApiController class can define methods that are mostly common to all controllers, such as methods for handling Json Response, and these methods can be overridden in other controllers to provide custom behavior.

By using a ApiController, developers can reduce the amount of code duplication and make it easier to maintain their application. Additionally, it can help enforce best practices and improve code organization by centralizing common functionality.

In our application, ApiController is created inside app/Http/Controllers/api folder which handles basic Transformation of collection/Multiple Collection/Model and pagination and error handling.

Functions

This code is a function that prepares a collection of data for a paginated JSON response . It uses the Fractal library to format the data, and it sets up the pagination information for the data using the Laravel paginator. The function takes a collection of data, a transformer function, and a key to use for the resource, and returns the paginated data as a JSON response.

protected function respondWithCollection($collection, $callback, $resourceKey)
  {
    $paginatedCollection = $collection->getCollection();

    $resource = new Collection($paginatedCollection, $callback, $resourceKey);

    $resource->setPaginator(new IlluminatePaginatorAdapter($collection));

    $rootScope = $this->fractal->createData($resource);
    return $this->metaEncode($rootScope->toArray());
  }

This code is a function that prepares a collection of data for a JSON response in a Laravel application without pagination information. It uses the Fractal library to format the data and returns the data as a JSON response.

 protected function respondWithOutPagination($collection, $callback, $resourceKey)
{
  $resource = new Collection($collection, $callback, $resourceKey);
  $rootScope = $this->fractal->createData($resource);
  return $this->metaEncode($rootScope->toArray());
}

This code is a function that prepares multiple collections of data for separate JSON responses . The function takes an array of collections, each with data, a transformer function, a key, and an optional setting for pagination. It uses the Fractal library to format the data for each collection and returns the data as separate JSON responses in a single final JSON response.

protected function respondWithMultipleCollection($collectionArray) {
  $resources['data'] = [];
  foreach($collectionArray as $collection) {
    $resource = new Collection($collection['collection'], $collection['callback'], $collection['key']);
    if(isset($collection['setPagination']) && $collection['setPagination']) {
      $resource->setPaginator(new IlluminatePaginatorAdapter($collection['collection']));
    }
    $data= $this->fractal->createData($resource)->toArray();
    $resources['data'][$collection['key']] = $data;
  }
  return $this->metaEncode($resources);
}

This function returns a json error response with a message of "Forbidden" and a HTTP status code of 403, indicating that the request is not allowed.

public function errorForbidden($message = 'Forbidden')
{
  return $this->setStatusCode(403)
    ->respondWithError($message, self::CODE_FORBIDDEN);
}

This function returns a json error response with a message of "Internal Error" and a HTTP status code of 500, indicating an internal server error. The HTTP status code can be changed by passing a different error code as an argument.

public function errorInternalError($message = 'Internal Error', $code = null)
{
  return $this->setStatusCode($code !== null ? $code : 500)
    ->respondWithError($message, self::CODE_INTERNAL_ERROR);
}

This function that returns a json error response with a message of "Resource Not Found" and a HTTP status code of 404, indicating that the requested resource could not be found.

  public function errorNotFound($message = 'Resource Not Found')
{
  return $this->setStatusCode(404)
    ->respondWithError($message, self::CODE_NOT_FOUND);
}


This function returns a json success response with a message of "OK" and a HTTP status code of 200, indicating a successful request.

public function responseOk($message = "OK")
{
  return $this->setStatusCode(200)
    ->respondWithSuccess($message);
}