Appearance
API Overview
An API is an interface that software developers use to programmatically interact with software components or resources outside of their own code. When building an API, a transformation layer is required that sits between Eloquent models and the JSON responses that are actually returned to the application's users. Know More About API
This section covers concepts about the API implementation. The cms uses the base api controller as ApiController which is located in app/Http/Controllers/Api folder.
Controller
class CategoryController extends ApiController
{
public function __construct(CategoryService $categoryService)
{
$this->service = $categoryService;
parent::__construct(new Manager);
}
public function index(Request $request)
{
$categories = $this->service->indexPageData($request);
return $this->respondWithCollection($categories['items'], new CategoriesTransformer, 'Categories');
}
public function detail($id)
{
$category = $this->service->singleData($id);
if ($category == null) {
return $this->errorNotFound();
}
return $this->respondWithItem($category, new CategoriesTransformer, 'Categories');
}
}
In this code, respondWithCollection is used to return collection of resources or a paginated response whereas respondWithItem return a single data.